<?php
header("Content-type:text/html;charset=utf-8");
//php生成百度站点地图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('数据库连接错误!');
mysql_select_db($CFG['db_name']);
mysql_query("set names utf8");
$p=$CFG['tb_pre'];
//查询并获取数据
$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 "数据读取成功!<br />";
$dom=new DomDocument('1.0', 'utf-8');
//urlset
$urlset=$dom->createElement('urlset');
$dom->appendchild($urlset);
echo "开始为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 "数据加载完毕。<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) {
// 创建元素
$$key = $dom->createElement($key);
$item->appendchild($$key);
// 创建元素值
$text = $dom->createTextNode($val);
$$key->appendchild($text);
if (isset($attribute[$key])) {
// 如果此字段存在相关属性需要设置
foreach ($attribute[$key] as $akey => $row) {
// 创建属性节点
$$akey = $dom->createAttribute($akey);
$$key->appendchild($$akey);
// 创建属性值节点
$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;
}
?>