Generate XML based on MySQL Query
Hello guys,
I need to create a XML file like this one(same structure) but based on MySQL query:
<chart>
<chart_type>pie</chart_type>
<chart_data>
<row>
<null/>
<string>2007</string>
<string>2008</string>
<string>2009</string>
</row>
<row>
<string>Region A</string>
<number>10</number>
<number>30</number>
<number>63</number>
</row>
</chart_data>
</chart>
I found a couple of good articles but I do not know how to get the same structure, I tried this:
<?php
header("Content-type: text/xml");
$host = "localhost";
$user = "test";
$pass = "test";
$database = "test";
$enlace = mysql_connect($host, $user, $pass) or die("Error MySQL.");
mysql_select_db($database, $enlace) or die("Error base de datos.");
$query = "SELECT AGENTE, count(*) FROM clientes group by agente";
$resultado = mysql_query($query, $enlace) or die("Sin resultados.");
$salida_xml = "<?xml version=\"1.0\"?>\n";
$salida_xml .= "<chart>\n";
$salida_xml .= "<chart_type>" . 'pie' . "</chart_type>\n";
$salida_xml .= "<chart_data>\n";
for($x = 0 ; $x < mysql_num_rows($resultado) ; $x++){
$fila = mysql_fetch_assoc($resultado);
$salida_xml .= "\t<row>\n";
$salida_xml .= "\t\t<agente>" . $fila['AGENTE'] . "</agente>\n";
$salida_xml .= "\t\t<cantidad>" . $fila['count(*)'] . "</cantidad>\n";
// Corregiendo caracteres incorrectos
$fila['texto'] = str_replace("&", "&", $fila['texto']);
$fila['texto'] = str_replace("<", "<", $fila['texto']);
$fila['texto'] = str_replace(">", ">", $fila['texto']);
// $salida_xml .= "\t\t<texto>" . $fila['texto'] . "</texto>\n";
$salida_xml .= "\t</row>\n";
}//segundo for
$salida_xml .= "</chart_data>\n";
$salida_xml .= "</chart>";
echo $salida_xml;
but I'm not getting the same structure, I am getting this:
<chart>
<chart_type>pie</chart_type>
−
<chart_data>
<row>
</row>
−
<row>
<agente>Danilo</agente>
<cantidad>8</cantidad>
</row>
<row>
</row>
−
<row>
<agente>Evelyn</agente>
<cantidad>5</cantidad>
</row>
<row>
</row>
−
<row>
<agente>Maribel</agente>
<cantidad>2</cantidad>
</row>
<row>
</row>
−
<row>
<agente>Nestor</agente>
<cantidad>11</cantidad>
</row>
<row>
</row>
−
<row>
<agente>Noemy</agente>
<cantidad>2</cantidad>
</row>
</chart_data>
</chart>
Can someone give me a hand plase?
Thanks in advance