user1411607
user1411607

Reputation:

How to effectively build sitemap using PHP?

I have build a simple site map script, i am not able to get URL output in URL field.

My PHP Script.

    header("Content-Type: text/xml;charset=iso-8859-1");
    echo '<?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
          xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
          http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    ';

    require_once('_ls-global/php/sr-connect.php');
    $db = mysql_select_db($database,$connection) or trigger_error("SQL", E_USER_ERROR);
    $sqlquery = mysql_query("SELECT * FROM $tablename ORDER by id")or die (mysql_error());

    while ($list = mysql_fetch_assoc($sqlquery)){

      $pflink=$list['pflink'];
      $pagelink=$list['pagelink'];

      $site="http://mysite.com";

      $url='$site/$pflink/$pagelink';

      $changefreq="weekly";
      $priority="1.0";

      echo '<url>
          <loc>'.$url.'</loc>
          <changefreq>'.$changefreq.'</changefreq>
          <priority>'.$priority.'</priority>
            </url>';

    }

    echo '</urlset>';

The Output of this script is this.

    <url>
    <loc>$site/$pflink/$pagelink</loc>
    <changefreq>weekly</changefreq>
    <priority>1.0</priority>
    </url>

If i change $url='$site/$pflink/$pagelink'; to $url="$site/$pflink/$pagelink";

then i get only one value and error "XML Parsing Error: not well-formed".

Please see and suggest any modification to make it work.

Thanks

Upvotes: 0

Views: 1513

Answers (3)

user1411607
user1411607

Reputation:

Based on thedom and FrontEndJohn answers and comment I got it right this way.

Changing $url='$site/$pflink/$pagelink'; to $url = $site .'/'. $pflink .'/'. $pagelink;

And modifying.

      echo '<url>
      <loc>'.$url.'</loc>
      <changefreq>'.$changefreq.'</changefreq>
      <priority>'.$priority.'</priority>
        </url>';

to

    echo '<url>';
echo '<loc><![CDATA['.$url.']]></loc>';
echo '<changefreq>'.$changefreq.'</changefreq>';
echo '<priority>'.$priority.'</priority>';
echo '</url>';

Hope this helps others too.

Upvotes: 1

thedom
thedom

Reputation: 2518

I guess you are having characters in the vars which are messing up the XML.

For example &, ä, <, >... You need to encode the content correctly.

Try wrapping the output: At first change $url to $url = $site .'/'. $pflink .'/'. $pagelink; and then update the output of the XML to:

<?php
// ...
      echo '<url>
          <loc><![CDATA['.$url.']]></loc>
          <changefreq>'.$changefreq.'</changefreq>
          <priority>'.$priority.'</priority>
            </url>';
?>

Explanation to CDATA available at http://en.wikipedia.org/wiki/CDATA

Upvotes: 1

JohnDevelops
JohnDevelops

Reputation: 567

If I understand your problem correctly, you cannot currently get the value of the variable due to the use of ' but when trying to use " so that the variables echo the XML gets upset.

Try:

$url = $site . '/' . $pflink . '/' . $pagelink;

This will give the value of the variables without using ". If I have miss-understood you please let me know.

Edit: Thinking about it, it looks more the the value of one or more of the variables may be what is upsetting the XML, assuming the variables are not giving their values while using '. It could be worth checking the contents of the variables for issues if you have not done so already.

Upvotes: 0

Related Questions