Share/Bookmark

Featured Posts

SharePoint Ticker Web Part from Existing Data Ever wanted to create a ticker in SharePoint without having to buy the $99 add-on from Microsoft, well here's the code: Simply create a new Web Part using the 'Content Editor' and open the 'Source Editor...'...

Read more

Sony E3 Prediction 2010 With E3 upon us in the coming weeks, and Sony keeping tight lipped about their schedule I think it's time we started predicting the outcome, the announcements and surprises with educated and industry sector...

Read more

Sony E3 Prediction 2010 With E3 upon us in the coming weeks, and Sony keeping tight lipped about their schedule I think it's time we started predicting the outcome, the announcements and surprises with educated and industry sector...

Read more

Whats next for Nintendo? Today I noticed that Nintendo have had falling profits for 2010 and predict even less sales for next year. The Wii 2 hasn`t been officially announced and from what I can see is not really on the radar....

Read more

7 Great Free Android Apps The Android mobile operating system is both fast, reliable and can do everything that the Apple OS can and a little more (all for normally a fraction of the price.) plus it supports Flash and soon Flash...

Read more

Creating a Sitemap in PHP from Dynamic Data

Posted by matthew | Posted in Code Snippets | Posted on 18-11-2009

Tags: , ,

1

Often you may have a site where a lot of the pages are created on the fly, in this case you need to make a sitemap to reflect this.
Below is a sitemap that includes static pages, then will list dynamic pages based on a database query. This is a very simple bit of code, but useful for a quick copy and paste.
<?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>";
?>
This query is making a url that looks like this:
http://www.mysite.com/joe+bloggs-person-staff-plonker/staff/5564.php
Notice how in the query we store the tags in the database separated by commas but when we create the URL, we replace these with dashes. We also encode the other items so that spaces and special characters are replaced.

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.

There are no priority tags used, as unless you want to add priority to certain pages it is a redundant tag to use. The sitemap will work with all good engines including Google.

(The most important thing to remember is the tag at the top to present the data as text/xml).

Comments (1)

[...] 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/) [...]

Write a comment