Search the Database (Mysql) and create Sitemap in parts (PHP)

Asked

Viewed 80 times

1

I have the following code to generate sitemap:

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

$sql = mysql_query("select aluno, matricula, aprovacao from faculdade");

while ($string = mysql_fetch_array($sql)){?>

<?php

$dataaprovacao = $string['aprovacao'];
$newDate = date("Y-m-d", strtotime($dataaprovacao));
?>

<url>
<loc>https://site.com.br/<?echo $string['aluno'];?>/<?echo $string['matricula'];?></loc>
<lastmod><?echo $newDate;?></lastmod>
</url>

<?php } ?> 
</urlset>

I would like to save this answer in several files (sitemap1.xml, sitemap2.xml etc.) with 50,000 urls each. It is possible?

  • It is possible yes. You can use the functions fwrite or file_put_contents to save the data to a file.

1 answer

0

I believe so. You can create a file called sitemap.php that gets a $_GET and work inside that file. Then simply enter a code similar to: RewriteRule ^sitemap([0-9]+).xml$ sitemap.php?id=$1 inside your file .htacess to have a custom dynamic url. In other words, your system would receive /sitemap1.xml URL and your code would interpret as /sitemap.php? id=1.

Browser other questions tagged

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