nezbo Posted July 30, 2008 Share Posted July 30, 2008 What is the easyest way to setup a XML site map... I am new to XML and i am not to sure how to go about it... I am looking at getting a php file to look at a of database and maybe the structure of the site and bring back a list of links for the XML, to put in to google sitemap. my site is www.darwenmtbclub.co.uk that i want to site map, and the list of rides are in a database. any ideas will be great, and code will be better Cheers I am not to sure if this is the correct forum that i have put it on, but as i want to have some php code that will do it automaticly for me i guesses PHP help wat teh base place Link to comment https://forums.phpfreaks.com/topic/117331-solved-setup-xml/ Share on other sites More sharing options...
unkwntech Posted July 30, 2008 Share Posted July 30, 2008 This is the format google uses: http://www.sitemaps.org/protocol.php Should be fairly simple, a MySQL query to get the pages, and a loop to build each one into the appropriate format. Link to comment https://forums.phpfreaks.com/topic/117331-solved-setup-xml/#findComment-603544 Share on other sites More sharing options...
nezbo Posted July 30, 2008 Author Share Posted July 30, 2008 Cool, Cheers, should i keep looping all the info in the database to creat one $string, i.e. one long $string? while ($onething = @mysql_query($infoFromServer)) { $string = $string . $onething['ride']; } How do i creat a file sitemap.xml file from the $string? Link to comment https://forums.phpfreaks.com/topic/117331-solved-setup-xml/#findComment-603554 Share on other sites More sharing options...
nezbo Posted July 30, 2008 Author Share Posted July 30, 2008 This is the code i have so far... $string1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"><url><loc>"; $string2 = "</loc><lastmod>" . date("Y-m-d", mktime()) . "</lastmod><changefreq>daily</changefreq><priority>0.8</priority></url></urlset>"; $siteMapArray = array(); $siteMapArray[0] = "http://" . $_SERVER['HTTP_HOST'] . "/"; $siteMapArray[1] = "http://" . $_SERVER['HTTP_HOST'] . "/rideList.php"; $siteMapArray[2] = "http://" . $_SERVER['HTTP_HOST'] . "/forum.php"; $siteMapArray[3] = "http://" . $_SERVER['HTTP_HOST'] . "/forumMes.php"; $numberOfBaseLinks = count($siteMapArray); for($i = 0; $i < $numberOfBaseLinks; $i++) { $XMLstring = $XMLstring . $string1 . $siteMapArray[$i] . $string2; } echo "<div align='center'>XML Created</div>"; Link to comment https://forums.phpfreaks.com/topic/117331-solved-setup-xml/#findComment-603573 Share on other sites More sharing options...
unkwntech Posted July 30, 2008 Share Posted July 30, 2008 Looks like your on the right track. You'll probably want to set the content type header to xml. header ("content-type: text/xml"); Don't forget to add this before any output. Link to comment https://forums.phpfreaks.com/topic/117331-solved-setup-xml/#findComment-603575 Share on other sites More sharing options...
nezbo Posted July 30, 2008 Author Share Posted July 30, 2008 the code i have so far I have got it saving a file but when i try and open a file it comes up with errors. I know you have said i need to add theis before any output header ("content-type: text/xml"); but i am not to sure what you mean. sorry... How can add this half way through the code? $string1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"><url><loc>"; $string2 = "</loc><lastmod>" . date("Y-m-d", mktime()) . "</lastmod><changefreq>daily</changefreq><priority>0.8</priority></url></urlset>"; $siteMapArray = array(); $siteMapArray[0] = "http://" . $_SERVER['HTTP_HOST'] . "/"; $siteMapArray[1] = "http://" . $_SERVER['HTTP_HOST'] . "/rideList.php"; $siteMapArray[2] = "http://" . $_SERVER['HTTP_HOST'] . "/forum.php"; $siteMapArray[3] = "http://" . $_SERVER['HTTP_HOST'] . "/forumMes.php"; $numberOfBaseLinks = count($siteMapArray); for($i = 0; $i < $numberOfBaseLinks; $i++) { $XMLstring = $XMLstring . $string1 . $siteMapArray[$i] . $string2; } $myFile = "sitemap.xml"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = $XMLstring; fwrite($fh, $stringData); fclose($fh); echo "<div align='center'>XML Sitemap Created</div>"; Link to comment https://forums.phpfreaks.com/topic/117331-solved-setup-xml/#findComment-603588 Share on other sites More sharing options...
unkwntech Posted July 30, 2008 Share Posted July 30, 2008 Sorry in this case you are fine and don't need to add the header. Link to comment https://forums.phpfreaks.com/topic/117331-solved-setup-xml/#findComment-603600 Share on other sites More sharing options...
nezbo Posted July 30, 2008 Author Share Posted July 30, 2008 code sorted $string0 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"; $string1 = "<url><loc>"; $string2 = "</loc><lastmod>" . date("Y-m-d", mktime()) . "</lastmod><changefreq>daily</changefreq><priority>0.8</priority></url>"; $string3 = "</urlset>"; $siteMapArray = array(); $siteMapArray[0] = "http://" . $_SERVER['HTTP_HOST'] . "/"; $siteMapArray[1] = "http://" . $_SERVER['HTTP_HOST'] . "/rideList.php"; $siteMapArray[2] = "http://" . $_SERVER['HTTP_HOST'] . "/forum.php"; $siteMapArray[3] = "http://" . $_SERVER['HTTP_HOST'] . "/forumMes.php"; $XMLstring = $string0; $numberOfBaseLinks = count($siteMapArray); for($i = 0; $i < $numberOfBaseLinks; $i++) { $XMLstring = $XMLstring . $string1 . $siteMapArray[$i] . $string2; } $XMLstring = $XMLstring . $string3; $myFile = "sitemap.xml"; $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, $XMLstring); fclose($fh); echo "<div align='center'>XML Sitemap Created</div>"; Link to comment https://forums.phpfreaks.com/topic/117331-solved-setup-xml/#findComment-603601 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.