我有一个用PHP生成的站点地图,实际上它只调用表零售商,如下所示:
$query = "select * from retailers WHERE status='active' ORDER BY added DESC";
并构造为:
while ($row = mysql_fetch_array($result))
{
$i_url = SITE_URL.'loja/'.$row['slug_title'];
$year = substr($row['added'],0,4);
$month = substr($row['added'],5,2);
$day = substr($row['added'],8,2);
$i_date = ''.$year.'-'.$month.'-'.$day.'';
// you can assign whatever changefreq and priority you like
// changefreg - optional
// priority - optional
echo
'
<url>
<loc>'.$i_url.'</loc>
<lastmod>'.$i_date.'</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
';
}
问题是,只有零售商的页面来了,我需要更多的3个表,但我想不出一种方法来调用和构建更多的内容,可能是PHP条件?
感谢大家的时间!
答案 0 :(得分:0)
我建议你创建一个函数来处理你需要的查询或子查询
像 主要代码
while ($row = mysql_fetch_array($result))
{
$i_url = SITE_URL.'loja/'.$row['slug_title'];
$year = substr($row['added'],0,4);
$month = substr($row['added'],5,2);
$day = substr($row['added'],8,2);
$i_date = ''.$year.'-'.$month.'-'.$day.'';
$data = subquery('what i need here', 'another param');
echo
'
<url>
<loc>'.$i_url.'</loc>
<lastmod>'.$i_date.'</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
';
}
function subquery($firstparam, $secondparam)
{
$myquery = "SELECT * FROM ".$firstparam;
//more code
$result = 'my query result';
return $result;
}
有了这个,您可以根据主查询调用子查询,您可以创建更多的功能或只创建一个具有不同类型的功能,可以在一个功能中执行不同的查询。
答案 1 :(得分:0)
由于这些表都具有我们需要的相同字段(slug_name
和added
),我们可以使用相同的过程循环遍历每个表,然后输出到sitemap.xml文件。
// Our Echo Buffer
$buffer = array();
// Table Names
$tables = array( 'retailers', 'table2', 'table3', 'table4' );
// Using MySQLi, cause it's Improved.
$conn = new MySqli( 'localhost', 'user', 'pass', 'database' );
// Iterate over $tables
foreach( $tables as $table )
{
// Build Query
$query = "SELECT `slug_name`, `added` FROM $table" .
" WHERE status='active' ORDER BY added DESC";
// Get Result
$result = $conn->mysqli_query( $query );
// Iterate over Result
while( $row = $result->fetch_assoc() )
{
// Chop up the Date
$date = substr($row['added'],0,4) . '-' .
substr($row['added'],5,2) . '-' .
substr($row['added'],8,2);
// Add page details to $buffer
$buffer[] = '<url>' .
'<loc>' . SITE_URL . 'loja/' . $row['slug_title'] . '</loc>' .
'<lastmod>' . $date . '</lastmod>' .
'<changefreq>daily</changefreq>' .
'<priority>0.8</priority>' .
'</url>';
}
// Free MySQLi Result
$result->close();
}
// Output the Buffer to view. Make sure it looks good.
echo implode( "\r\n", $buffer );
// Remove the echo above and uncomment below if it looks good.
// if( ( $xml = fopen( 'sitemap.xml', "w" ) ) !== FALSE )
// {
// fwrite( $xml, implode( "\r\n", $buffer ) );
// }