0 Replies - 1316 Views - Last Post: 28 May 2010 - 07:16 PM

#1 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12324
  • View blog
  • Posts: 45,424
  • Joined: 27-December 08

Write Columns of Table to XML

Posted 28 May 2010 - 07:16 PM

Description: Writes SQL SELECT Query to an XML File, writing only the columns passed in the array.
/*
   @param $result: the result set from the SQL query
   @param $cols: the array of column names
   @param $filename: the name of the File to create
   @param $tagName: The name of the container tag for each row
   <pre>The File is assumed to not exist</pre>
*/
public function queryToXML($result, array $cols, $filename, $tagName){
    $file = fopen($filename, "w"); //open up file stream

    //write initial XML heading
    fwrite($file, "<?xml version="1.0" encoding="ISO-8859-1"?>n");
    
    //foreach row
    while($row = mysql_fetch_array($result)){
        fwrite($file, "<".$tagName.">n");
        
        //write data using col name as tag
        foreach($cols as $tag){
             fwrite($file,"t<".$tag.">".$row[$tag]."</".$tag.">n");
        }//end foreach
         
        //close row tag
        fwrite($file,"</".$tagName.">n"); 
    }//end while

    fclose($file);
}//end queryToXML()


Is This A Good Question/Topic? 0
  • +

Page 1 of 1