Creating a Sitemap in PHP from Dynamic Data
Posted by matthew | Posted in Code Snippets | Posted on 18-11-2009
Tags: mySQL, PHP, XML
1
<?php
include '/home/vhosts/mysite.com/includes/db.inc';
header('Content-type: text/xml');
echo "<?xml version='1.0' encoding='UTF-8'?>\r\n";
echo "<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>\r\n";
echo "<url><loc>http://www.mysite.com/index.php</loc></url>\r\n";
echo "<url><loc>http://www.mysite.com/about.php</loc></url>\r\n";
echo "<url><loc>http://www.mysite.com/contact.php</loc></url>\r\n";
echo "<url><loc>http://www.mysite.com/terms.php</loc></url>\r\n";
$sql = "SELECT * FROM mytable WHERE hide != 'yes' ORDER BY name ASC";
$result = mysql_query($sql);
$numr = mysql_num_rows($result);
if($numr != '0') {
while($row = mysql_fetch_array($result)) {
$temptype = str_replace(",","-",$row['type']);
echo "<url><loc>http://www.mywebsite.com/".urlencode($row['name'])."-".urlencode($type)."/".$row['tag']."/".$row['code'].".php</loc></url>\r\n";
}
}
echo "</urlset>";
?>
http://www.mysite.com/joe+bloggs-person-staff-plonker/staff/5564.php
This example is good for an Apache rewrite that is simply looking for the code at the end, as then all the data before can be anything we want.
(The most important thing to remember is the tag at the top to present the data as text/xml).




[...] This post was mentioned on Twitter by Carboard Coder, Carboard Coder. Carboard Coder said: New post: Creating a Sitemap in PHP from Dynamic Data (http://computerf6.info/2009/11/creating-a-sitemap-in-php-from-dynamic-data/) [...]