Question

I have a sitemap that's generated with PHP and actually its calling only the table retailers as below:

$query = "select * from retailers WHERE status='active' ORDER BY added DESC";

and construct as:

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>
    ';
}

The problem is, only retailers page its coming, i need to get some more 3 tables, but i can't think on a way to call and construct more things inside that, maybe a PHP condition?

Thanks to everyone for your time!

Was it helpful?

Solution

I suggest you to create a function to handle the queries or subqueries you need

Like Main Code

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;
}

With this you can call a subquery based on the main query, you can create more funcionts or create only one with different types that, can able you to do different queries in one function.

OTHER TIPS

Since the tables all have the same fields we need (slug_name and added), we can just loop through each table with the same procedure, then output to sitemap.xml file.

    // 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 ) );
    // }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top