Question

I came up with a PHP version of a sitemap file for my dynamic site which only has index.php. Here is the result: http://www.oddsnavigator.eu/sitemap.php

Now I need a way to either export those results to sitemap.xml file, or to make this one XML and Google-friendly. Here's the code:

<?PHP
$SQL="boring query";
$num_rows = mysql_num_rows(mysql_query($SQL));
echo $num_rows , "</BR>";
$result = mysql_query($SQL);
if($result === FALSE) { die(mysql_error());}

while($db_field=mysql_fetch_assoc($result)) {
$goto = "http://oddsnavigator.eu/?display=0&sport=" . $db_field['sportid'] . "&day=all&league=" . $db_field['stageid'];
$title = $db_field['sportname'] . " " . $db_field['tempname'] . " " . $db_field['stagename'] . " Sports Betting Odds Comparison - OddsNavigator.eu";

echo "<a href='$goto' title='$title'>" , $db_field['sportname'] , " " , $db_field['tempname'] , " " , $db_field['stagename'] , "</a>" , "</BR>";
}
?>

Any ideas?

Was it helpful?

Solution

In Your code $goto contains URLs, store all these url in an array called $urls and then use following function.
I expect the code goes in class, if not replace $this->variable with $variable.

//$urls = array();
$compress = true;

function save () 
{
    if (empty($this->urls)) return;
    $file = "sitemap.xml{$this->compress}";
    $xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
    $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
    foreach ($this->urls as $url) 
    {
        $xml .= '  <url>' . "\n";
        if (is_array($url)) {
            foreach ($url as $key => $value) $xml .= "    <{$key}>{$value}</{$key}>\n";
        } 
        else {
            $xml .= "    <loc>{$url}</loc>\n";
        }
        $xml .= '  </url>' . "\n";
    }
    $xml .= '</urlset>' . "\n";
    $this->urls = array();
    if (!empty($this->compress)) $xml = gzencode($xml, 9);
    $fp = fopen(BASE_URI . $file, 'wb');
    fwrite($fp, $xml);
    fclose($fp);
}

OTHER TIPS

And here's the full code of the solution, for future reference if anyone is having the same problem. The code uses SQL query to extract information from the database and then passes is on into a XML file.

<?PHP

$SQL="boring query";

$result = mysql_query($SQL);

//assigning the value of the URL which will be moved to sitemap.xml
while($db_field=mysql_fetch_assoc($result)) {
$urls[] = "http://oddsnavigator.eu/?display=0&sport=" . $db_field['sportid'] . "&day=all&league=" . $db_field['stageid'];
}

mysql_close($db_handle);

$compress = true;
//setting the sitemap.xml header
$xml = "<?xml version='1.0' encoding='UTF-8'?>" . "\n";
$xml .= "<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>" . "\n";

//adding each element
foreach ($urls as $url) 
{
$xml .= '  <url>' . "\n";
if (is_array($url)) {
foreach ($url as $key => $value) $xml .= "    <{$key}>{$value}</{$key}>\n";
} 
else {
$xml .= "    <loc>{$url}</loc>\n";
$xml .= "    <changefreq>hourly</changefreq>\n";
}
$xml .= '  </url>' . "\n";
}
$xml .= '</urlset>' . "\n";
$urls = array();

//writing to file
$fp = fopen('sitemap.xml', 'wb');
fwrite($fp, $xml);
fclose($fp);
?>

You could use a template engine such as Smarty.

That would allow you to easily loop through the creation of the XML.

Or you could use [http://php.net/manual/en/book.simplexml.php SimpleXML].

If you already use a template engine, that would probably be the quickest and easiest solution. Otherwise, I would use SimpleXML.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top