How to generate simple sitemap.xml using php and mysql


Posted on 14 November 2014 at 05:11 PM


define('HOST', 'http://' . $_SERVER['SERVER_NAME']);
define('CRLF', "\r\n");
define('TAB', "\t");
define('TAB2', "\t\t");


function makes_sitemap($sitemap = 'sitemap.xml')
{
     $content = '' . CRLF . '' . CRLF;     
     $sql = 'SELECT * FROM posts ORDER BY post_date DESC;';
     $res = mysql_query($sql);
     $format = '%s' . CRLF . TAB . '' . CRLF . TAB2 . '' . HOST . '/%s' . CRLF . TAB2 . '%s' . CRLF . TAB2 . 'monthly' . CRLF . TAB2 . '0.8








' . CRLF . TAB . '';
     while($arr = mysql_fetch_assoc($res)){
         $dat = explode(' ', $arr['post_date']);
         $content = sprintf($format, $content, $arr['post_slug'], $dat[0]);
         }
     $content = $content . CRLF . '' . CRLF;
     file_put_contents($sitemap, $content);
     }

Make sure you have write access on the directory where the file is created


Leave a Comment: All fields are mandatory !


* Maximum 32 charactes
* Your email will never be made public
* All HTML tags are automatically stripped down !

Stats

Posts: 10
Comments: 4
Files: 9
Downloads: 1501