XML repeats record when generated in PHP

Asked

Viewed 44 times

0

I have a PHP script that generates XML file, but something very strange is that it repeats several records of my real estate table and the worst thing is that there are no repeated properties. Repeats the property ID where there is only one property for each ID.

Follow my code that generates XML:

$arquivo = "nuroa.xml";
$ponteiro = fopen($arquivo, "w");
fwrite($ponteiro, "<?xml version='1.0' encoding='utf-8'?>");
fwrite($ponteiro, '<nuroa>');

$sqlguiatexto = "SELECT id FROM imoveis";
$rsqlguiatexto = mysql_query($sqlguiatexto)
or die ("Não foi possível realizar a consulta ao banco de dados");

while($roweditusertexto = mysql_fetch_array($rsqlguiatexto))
   {

       $conteudo .= '<ad>';
       $conteudo .= '<id><![CDATA['.$roweditusertexto['id'].']]></id>';
       $conteudo .= '</ad>';
       fwrite($ponteiro, $conteudo);

   }

fwrite($ponteiro, '</nuroa>');
fclose($ponteiro);

Can anyone tell me what’s going on for this? Strange is that there is no repeat record.

I’m waiting for help, thank you very much!

1 answer

2


You are adding the content repeatedly to the file.

See the difference within the WHILE:

$arquivo = "nuroa.xml";
$ponteiro = fopen($arquivo, "w");
fwrite($ponteiro, "<?xml version='1.0' encoding='utf-8'?>");
fwrite($ponteiro, '<nuroa>');

$sqlguiatexto = "SELECT id FROM imoveis";
$rsqlguiatexto = mysql_query($sqlguiatexto)
or die ("Não foi possível realizar a consulta ao banco de dados");

while($roweditusertexto = mysql_fetch_array($rsqlguiatexto))
   {
       $conteudo  = '<ad>';
       //        ↑ aqui não vai o ponto, pois é um conteúdo novo.
       $conteudo .= '<id><![CDATA['.$roweditusertexto['id'].']]></id>';
       $conteudo .= '</ad>';
       fwrite($ponteiro, $conteudo);

   }

fwrite($ponteiro, '</nuroa>');
fclose($ponteiro);
  • How do I solve?

  • 1

    @Gladisonneuzaperosini the code in the answer is already the solution. Instead of just adding things up in the content, we are recreating it with each data. Look at the difference in $conteudo of the first line. In the first line = in place of .= - I edited the comment in the code for you to find the difference.

  • Thank you very much! It worked perfectly.

Browser other questions tagged

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