-
-
Save JMDTol/7a0090f0dab6e30a26427d4ad2812c4d to your computer and use it in GitHub Desktop.
Sitemaps in SimpleXML in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('Content-type: text/xml'); | |
//connect to db | |
$link=mysql_pconnect($db['write']['host'],$db['write']['user'],$db['write']['pass']) or die ("Could not connect to datadase"); | |
mysql_select_db($db['write']['name']) or die ("could not select database"); | |
$base='http://'.$_SERVER['HTTP_HOST']; | |
$xml = new SimpleXMLElement("<?xml version='1.0' encoding='UTF-8' ?>\n".'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" />'); | |
//main page | |
$url = $xml->addChild('url'); | |
$url->addChild('loc',$base.'/'); | |
//$url->addChild('lastmod',gmdate('c',filemtime('main.html'))); | |
$url->addChild('priority','1.0'); | |
//pages | |
$query="select pagename, unix_timestamp(staticpage.modified) as modified from page"; | |
$result=mysql_query($query); | |
if ($result) { | |
while ($line=mysql_fetch_assoc($result)) { | |
$url = $xml->addChild('url'); | |
$url->addChild('loc',$base.'/'.$line['pagename']); | |
if ($line['modified']!=0) $url->addChild('lastmod',gmdate('c',$line['modified'])); | |
$url->addChild('priority','0.5'); | |
} | |
} | |
//category | |
$query="select * from category"; | |
$result=mysql_query($query); | |
if ($result) { | |
while ($line=mysql_fetch_assoc($result)) { | |
$url = $xml->addChild('url'); | |
$entryurl='/category/'.$line['categorysafename']; | |
$url->addChild('loc',$base.$entryurl); | |
$url->addChild('priority','0.8'); | |
} | |
} | |
//articles | |
$query="select article.*,unix_timestamp(article.modified) as modified from article"; | |
$result=mysql_query($query); | |
if ($result) { | |
while ($line=mysql_fetch_assoc($result)) { | |
$url = $xml->addChild('url'); | |
$entryurl='/article/'.$line['pagename']; | |
$url->addChild('loc',$base.$entryurl); | |
// some entries had a missing modified data, this only adds it if present. | |
if ($line['modified']!=0) $url->addChild('lastmod',gmdate('c',$line['modified'])); | |
$url->addChild('priority','0.6'); | |
} | |
} | |
//feeds | |
$url = $xml->addChild('url'); | |
$url->addChild('loc',$base.'/events/rss'); | |
$url->addChild('priority','0.6'); | |
$query="select * from blogcategory"; | |
$result=mysql_query($query); | |
if ($result) { | |
while ($line=mysql_fetch_assoc($result)) { | |
$url = $xml->addChild('url'); | |
$url->addChild('loc',$base.'/'.$line['categoryname'].'/rss'); | |
$url->addChild('priority','0.6'); | |
} | |
} | |
$output=$xml->asXML(); | |
//removed caching and output encoding code. | |
echo $output; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment