0

我有一个使用 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 条件?

感谢大家的时间!

4

2 回答 2

0

由于表都有我们需要的相同字段(slug_nameadded),我们可以用相同的过程遍历每个表,然后输出到 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 ) );
    // }
于 2013-10-10T19:04:50.690 回答
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;
}

有了这个,你可以在主查询的基础上调用一个子查询,你可以创建更多的函数或只创建一个具有不同类型的函数,可以让你在一个函数中执行不同的查询。

于 2013-10-10T18:21:11.627 回答