-1

I'm working on outputting data from MySQL database in XML format, so I made a php file that displays data from database in XML format on the page.

<?php
// Return all existing nid in array (Different types of form);
function nid(){
    $query = "SELECT w.nid 
                FROM webform w";
    $result = mysql_query($query);
    $table = array();
    while ($row = mysql_fetch_row($result)){
        $table[] = $row[0];
    }
    return $table;
}

// Return existing rows of corresponding nid
function sid($nid){
    $query = "SELECT ws.sid 
                FROM webform_submissions ws 
                WHERE ws.nid = $nid";
    $result = mysql_query($query);
    $table = array();
    while ($row = mysql_fetch_row($result)){
        $table[] = $row[0];
    }
    return $table;  
}
// Return corresponding components of nid;
function cid($nid){
    $query = "SELECT wc.form_key 
                FROM webform_component wc 
                WHERE wc.nid = $nid
                ORDER BY wc.cid ASC";
    $result = mysql_query($query);
    $table = array();
    while ($row = mysql_fetch_row($result)){
        $table[] = $row[0];
    }
    return $table;  
}

// Return values of fields corresponding to nid and sid
function data($nid, $sid){
    $query = "SELECT wsd.data 
                FROM webform_submitted_data wsd 
                WHERE wsd.nid = $nid AND wsd.sid = $sid
                ORDER BY wsd.sid ASC";
    $result = mysql_query($query);
    $table = array();
    while ($row = mysql_fetch_row($result)){
        $table[] = $row[0];
    }
    return $table;      
}
// Define Constants
DEFINE("DB_SERVER", "localhost");
DEFINE("DB_USER", "root");
DEFINE("DB_PASS", "");
DEFINE("DB_NAME", "drupal7");

//Establish Connection
mysql_connect(DB_SERVER, DB_USER, DB_PASS)
    or die("Unable to connect to MySQL");

//Select Database
mysql_select_db(DB_NAME)
    or die("Could not connect to the database");

$xml_output = "<?xml version=\"1.0\" ?>\n";
$xml_output .= "<schema>";

foreach (nid() as $nid){
    $xml_output .= "<form fid=\"$nid\">\n";
    foreach (sid($nid) as $sid){
        $xml_output .= "<row rid=\"$sid\">\n";
        for ($i = 0; $i < count(cid($nid)); $i++){
            $tag_array = cid($nid);
            $value_array = data($nid, $sid);
            $tag_row = $tag_array[$i];
            $value_row = $value_array[$i];
            $xml_output .= "<$tag_row>\n";
            $xml_output .= "$value_row\n";
            $xml_output .= "</$tag_row>\n";
        }
        $xml_output .= "</row>\n";
    }
    $xml_output .= "</form>\n";
}
$xml_output .= "</schema>";

header("Content-type: text/xml");
echo $xml_output;
mysql_close();

?>

But I can't see to find a way to access the XML data or download it as XML file.

Here's how the output looks like when I run it. enter image description here

Thanks in advance

lusketeer
  • 1,890
  • 1
  • 12
  • 29

2 Answers2

1

Don't use strings to build your xml data. You should be using something like SimpleXMLElement

See How to generate XML file dynamically using PHP?

Community
  • 1
  • 1
Hans Z
  • 4,664
  • 2
  • 27
  • 50
0

Instead of:

$table[] = $row[0];

Try using the array_push function:

array_push($table,$row[0]);
ckpepper02
  • 3,297
  • 5
  • 29
  • 43