Forum Moderators: coopster
Php Code
=================================
<?php
require_once('config.php');
mysql_connect($server,$user,$pass);
mysql_select_db($database);
$sql="SELECT * FROM sitemap";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
$lastid=$row[0]; //This is the value of the last URL id that was entered successfully to sitemap in my case its on 1 as its starting from begining
$sql_url=mysql_query("SELECT * FROM books ORDER BY id ASC LIMIT $lastid, 50");
//Using the loop to store the urls
while($row_url = mysql_fetch_array($sql_url))
{
$pattern = "/[^A-Za-z0-9\-]/";
$url_row2=str_replace(' ','-',$row_url[2]);
$url_row3=str_replace(' ','-',$row_url[3]);
$url_row2= preg_replace($pattern,'',$url_row2);
$url_row3= preg_replace($pattern,'',$url_row3);
$xml_url.='<url>';
$xml_url.='<loc>http://www.site.com/textbook/'.$row_url[11].'-'.$url_row2.'-by-'.$url_row3.'-ISBN:'.$row[1].'</loc>';
$xml_url.='<changefreq>never</changefreq>';
$xml_url.='</url>';
}
// I am also stuck here
// This is how my .xml file is right now, I somehow need to read the <!-- URLS Below --> and add the xml urls below that, not sure how to do that in php
+++++++++++++++++++++++++++++++++++++++++++++++
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<!-- URLS Below -->
<url>
<loc>http://www.example.com/</loc>
<changefreq>never</changefreq>
</url>
</urlset>
+++++++++++++++++++++++++++++++++++++++++++++++++
$fp = fopen ("/site/sitemap.xml", "w");
//help needed here
//update the last done field
$lastidone=$lastid+50;
mysql_query("UPDATE sitemap SET last_done ='.$lastdone.'");
?>
The way I do my automated sitemaps in PHP is:-
- setup a mod_rewrite rule to point sitemap.xml -> sitemap.php
- have a 'lastmod' column on pages/books which is automatically updated on every edit (via mysql UPDATE CURRENT_TIMESTAMP)
- MAKE SURE YOU ENCRYPT FOR XML (& etc)
- output the XML sitemap in PHP
- order it homepage, categories, products, pages
This way you won't need a cronjob - it's run on the fly