Sitemap Mysql PHP divided into result quantities

Asked

Viewed 83 times

2

<?php
header("Content-type: text/xml");
echo'<?xml version=\'1.0\' encoding=\'UTF-8\'?>';
echo'   <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9     http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';

include 'conexao.php';
include 'acoes.php';

$sql = mysql_query("select nome, nota from aluno");

while ($string = mysql_fetch_array($sql)){?>
            <url>
                <loc>http://www.escola.com.br/<?echo     removerosAcentos($string['nome']);?>/<?echo $string['nota'];?></loc>
                <changefreq>weekly</changefreq>
            </url>
<?php } ?> 
</urlset>

This code generates a sitemap (pulling from the database)

I need to generate a sitemap with 45 thousand (sitemap1.xml) and after reaching the 45 thousand results another sitemap (sitemap2.xml) and so on.

There is a column with the name "number" (Ex: 054322123) that can be used to sort. I have no idea how to do this. Can anyone help me in detail? I appreciate!

1 answer

1


If you run a counter no longer solves?

<?php

include 'conexao.php';
include 'acoes.php';

$sql = mysql_query("select nome, nota from aluno");

$a = 0;
while ($string = mysql_fetch_array($sql))
{
    if ($a == 0)
    {
        $data = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9     http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
        $data .= '<?xml version=\'1.0\' encoding=\'UTF-8\'?>';
        $data .= '<urlset>';
    }

    $data .= '<url>';
    $data .= '<loc>http://www.escola.com.br/' . removerosAcentos($string['nome']);
    $data .= $string['nota'] . '</loc>';
    $data .= '<changefreq>weekly</changefreq>';
    $data .= '</url>';
    $a += 1;
    if ($a == 45.000)
    {
        $data .= '</urlset>';
        $a = 0;

        $nome_arquivo = uniqid();
        header('Content-disposition: attachment; filename="' . $nome_arquivo . '.xml"');
        header('Content-type: "text/xml"; charset="utf8"');
        readfile($nome_arquivo . '.xml');
 // Os dados do arquivo estão presos na variável $data, basta usar os dados dela pra inserir as informações na XML
//Aqui você faz a saída individual pra cada arquivo de 45.000 registros
    }
}
?> 

This way it is now, every 45.00 lines will be generated a new id to name a new file, but Voce will need to study how to generate this individual xml with PHP, you can use several classes, I believe Xmlwritter is the easiest.

  • Anthraxisbr, How would I look in my code above (I really don’t have much sense) And how would I be created automatically? sitemap1.xml sitemap2.xml etc? Can you detail more? Thank you.

  • Take a look at the response update, now you should be able to do this

  • I’m trying to mount on top of your code. So far nothing, in case you get put here the solution! Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.