Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Generate XML based on MySQL Query

New Here ,
Apr 02, 2011 Apr 02, 2011

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("<", "&lt;", $fila['texto']);
            $fila['texto'] = str_replace(">", "&gt;", $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

TOPICS
Server side applications
443
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 03, 2011 Apr 03, 2011
LATEST

Doing some extra research I almost get to the result that I need, I guess I am jus

t having an small problem.

Basically I need this

[PHP]
<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>
[/PHP]

Currently I have this:

[PHP]
<chart>
    <chart_type>pie</chart_type>

    <chart_data>
        <row>
            <null></null>
            <agente>Danilo</agente>
            <agente>Evelyn</agente>

            <agente>Maribel</agente>
            <agente>Nestor</agente>
            <agente>Noemy</agente>
        </row>
        <row>
            <rutas>rutas</rutas>
            <cantidad></cantidad>

            <cantidad></cantidad>
            <cantidad></cantidad>
            <cantidad></cantidad>
            <cantidad></cantidad>
        </row>
    </chart_data>
</chart>
[/PHP]

And I am using this code

[PHP]<?php

header("Content-type: text/xml");

$host = "xxx";
$user = "xxxx";
$pass = "xxxx";
$database = "xxxx";

$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 WHERE PREVENTA LIKE 'Viernes' 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 .= "\t<chart_type>" . 'pie' . "</chart_type>\n\n";
$salida_xml .= "\t<chart_data>\n";
            $salida_xml .= "\t\t<row>\n";
            $salida_xml .= "\t\t\t<null>" . $fila5['NULL']. "</null>\n";
            for($y = 0 ; $y < mysql_num_rows($resultado) ; $y++){
                $fila2 = mysql_fetch_assoc($resultado); 
                $salida_xml .= "\t\t\t<agente>" . $fila2['AGENTE'] . "</agente>\n";
                }//cierro for Y
            $salida_xml .= "\t\t</row>\n";
            $salida_xml .= "\t\t<row>\n";    
            $salida_xml .= "\t\t\t<rutas>" . 'rutas'. "</rutas>\n";
            for($z = 0 ; $z < mysql_num_rows($resultado) ; $z++){
                $valor = mysql_fetch_assoc($resultado);
                $salida_xml .= "\t\t\t<cantidad>" . $valor['count(*)'] . "</cantidad>\n";
            }//cierro for z

            $salida_xml .= "\t\t</row>\n";
    $salida_xml .= "\t</chart_data>\n";
   
$salida_xml .= "</chart>";

echo $salida_xml;

?>[/PHP]

The issue I am having is that it is not showing the quantity, in this case "cantidad", does someone know why?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines