<?php
Header ("Content-type:text/html;charset=utf-8");
PHP build Baidu Sitemap Sitemap.xml
Http://www.baidu.com/search/sitemaptools_help.html
Include (".. /config.inc.php ");
mysql_connect ($CFG [' Db_host '], $CFG [' Db_user '], $CFG [' Db_pass ']) or Die (' Database connection Error! ‘);
mysql_select_db ($CFG [' db_name ']);
mysql_query ("Set names UTF8");
$p = $CFG [' Tb_pre '];
Querying and getting data
$query = "Select ' Linkurl ', ' editdate ' from ' {$p}table ' where ' status ' = ' 3 ' ORDER by ' itemid ' desc limit 1000";
$result =mysql_query ($query);
$m = 0;
while ($info =mysql_fetch_array ($result, Mysql_assoc)) {
$data _array[$m] [' Loc ']= ' http://www.example.net/aaa/'. $info [' Linkurl '];
$data _array[$m] [' Lastmod ']= $info [' editdate '];
$data _array[$m] [' changefreq ']= ' daily ';
$data _array[$m] [' priority ']= ' 0.8 ';
$m + +;
}
echo "Data read successfully! <br/> ";
$dom =new DomDocument (' 1.0 ', ' utf-8 ');
Urlset
$urlset = $dom->createelement (' Urlset ');
$dom->appendchild ($urlset);
echo "begins loading data for XML. <br/> ";
$attribute _array=array ();
foreach ($data _array as $data) {
$url = $dom->createelement (' url ');
$urlset->appendchild ($url);
Create_item ($dom, $url, $data, $attribute _array);
}
echo "Data loading is complete. <br/> ";
$filesize = $dom->save (".. /sitemaps/sell_sitemap.xml ");
echo "Sell_sitemap Create success! <br/> ";
echo "Size:". Changefilesize ($filesize). " <br/> ";
echo "<a href= ' Javascript:window.history.back (); ' >go back</a><br/> ";
echo "<a href=". /sitemaps/sell_sitemap.xml ' target= ' _blank ' >view it</a> ';
function Create_item ($dom, $item, $data, $attribute) {
if (Is_array ($data)) {
foreach ($data as $key = = $val) {
Creating elements
$ $key = $dom->createelement ($key);
$item->appendchild ($ $key);
Creating element values
$text = $dom->createtextnode ($val);
$ $key->appendchild ($text);
if (Isset ($attribute [$key])) {
If there are related attributes in this field, you need to set
foreach ($attribute [$key] as $akey = = $row) {
To create an attribute node
$ $akey = $dom->createattribute ($akey);
$ $key->appendchild ($ $akey);
Creating attribute Value Nodes
$aval = $dom->createtextnode ($row);
$ $akey->appendchild ($aval);
}
}//End If
}
}//End If
}//End Function
function Changefilesize ($filesize)
{
if ($filesize >= 1073741824)
{
$filesize = Round ($filesize/1073741824, 2). ' Gb ';
} elseif ($filesize >= 1048576)
{
$filesize = Round ($filesize/1048576, 2). ' Mb ';
} elseif ($filesize >= 1024)
{
$filesize = Round ($filesize/1024, 2). ' Kb ';
} else
{
$filesize = $filesize. ' Bytes ';
}
return $filesize;
}
?>
PHP build Baidu Sitemap Sitemap.xml