PHP, Array, and XML tree

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nmpku
Forum Newbie
Posts: 11
Joined: Wed Oct 05, 2005 12:42 am

PHP, Array, and XML tree

Post by nmpku »

ok, so i need to loop through each row in my table and output the data in xml format to a file. Essentially making numerous files. the code is below. i am having trouble doing this. please take a look and see if you can assist

Code: Select all

<?php
// output basic header info and beginning of xml tags
$output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

$output .= "<Assets>";

$result_story = mysql_query( "Select story.storyid, story.created, story.startdate, story.headline, story.subhead, story.byline, story.body, story.comments, story.isactive, story.keepalive, story.fromprint, story.siteid, topic.topicname FROM story, storytopic, topic WHERE story.isactive = 'Y' AND story.storyid=storytopic.storyid AND storytopic.topicid = topic.topicid", $dbhandle );
$totalrows = mysql_num_rows($result_story);

$i=0;

for($i=0; $i <= $totalrows; $i++){
     $row = mysql_fetch_assoc($result_story[$i]);
     $output .= "<Asset>";
    $output .= "<AssetType>Story</AssetType>";
    $output .= "<Fields>";
          foreach($row as $name=>$value){
          $output .= "<Field>";
            $output .= "<Name>" .htmlentities($name);
            $output .= "</Name>";
            $output .= "<Values>";
            $output .= "<Value>" .htmlentities($value)."</Value>";
            $output .= "</Values>";
            $output .= "</Field>";
                            
          }
      $output .= "</Fields>";    
      $output .= "</Asset>";      
}
$output .= "</Assets>";
       
// tell the browser what kind of file is come in
header("Content-type: text/xml");
// print out XML that describes the schema
echo $output;

// close the connection
mysql_close($dbhandle);

?>
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Code: Select all

     $row = mysql_fetch_assoc($result_story[$i]); 
$result_story is not an array, why are you trying to call it with $result_story[$i];

You probably want to fetch the query as an array and then loop threw it.

Code: Select all

$query = 'SELECT * FROM my_table'; // Sample Query
$result = mysql_query($query) or die('Query failed: ' . mysql_error()); // You dont even "submit" the query in your code

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "<tr>";
    foreach ($line as $col_value) {
        echo "<td>$col_value</td>";
    }
    echo "</tr>";
}
echo "</table>"
nmpku
Forum Newbie
Posts: 11
Joined: Wed Oct 05, 2005 12:42 am

Post by nmpku »

thanks zox...that got me headed in the right direction.
nmpku
Forum Newbie
Posts: 11
Joined: Wed Oct 05, 2005 12:42 am

Post by nmpku »

Previous problem solved...new one here.

i can now loop through my records, and output it into xml (for the most part). However, i need to write each row to a new file. I am having trouble with that, as I keep getting "HTTP Wrapper Errors" and FTP Errors"

Please assist!


Code Snippet!

Code: Select all

<?php

while($row = mysql_fetch_array($result_story, MYSQL_ASSOC)) {
 	$output .= "<Asset>";
	$output .= "<AssetType>Story</AssetType>";
	$output .= "<Fields>";
  		foreach($row as $name=>$value){
  			$output .= "<Field>";
    		        $output .= "<Name>" .htmlentities($name);
    		        $output .= "</Name>";
    		        $output .= "<Values>";
    		        $output .= "<Value>" .htmlentities($value)."</Value>";
    		        $output .= "</Values>";
    		        $output .= "</Field>";
    	        }
  	$output .= "</Fields>";	
  	$output .= "</Asset>";
  	$fh=fopen("ftp://username:password@serveraddress/".$row["storyid"] .".xml","w");
	fwrite($fh,utf8_encode($output));
	fclose($fh); 
}

?>
nmpku
Forum Newbie
Posts: 11
Joined: Wed Oct 05, 2005 12:42 am

Post by nmpku »

i got it working...seems it was working, but had to reboot my system, for some reason, in order to see the transfer
nmpku
Forum Newbie
Posts: 11
Joined: Wed Oct 05, 2005 12:42 am

Post by nmpku »

i thought i had it, til i looked and it is not the right format:

ok....i am on my third bottle now....absolutely driving me up the wall. Here is my dilemma. I need <Asset> to be the root, I can not have it outside of my loop, because i need that to be the root of every record. Then I need to loop through the records, and get every row, and write them to a file. Everytime I place <Asset> inside my "outer loop" it comes up with "XML JUNK Error." (Note - I need it to output into an xml file) I can get it to echo fine (see code below) with the exception of the fact that it echos all records in one file. I need separate files for each record. How would I change it to loop through to accommodate my situation.

Code:

Code: Select all

// output basic header info and beginning of xml tags
$output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

$output .= "<Asset>";

$result_story = mysql_query( "Select story.storyid, story.created, story.startdate, story.headline, story.subhead, story.byline, story.body, story.comments, story.isactive, story.keepalive, story.fromprint, story.siteid, topic.topicname FROM story, storytopic, topic WHERE story.isactive = 'Y' AND story.storyid=storytopic.storyid AND storytopic.topicid = topic.topicid LIMIT 0,5000", $dbhandle );
$totalrows = mysql_num_rows($result_story);

$i=0;

while($row = mysql_fetch_array($result_story, MYSQL_ASSOC)) {
 			
// 			$output .= "<Asset>";
			$output .= "<AssetType>Story</AssetType>";
			$output .= "<Fields>";
 		foreach($row as $name=>$value){
  			$output .= "<Field>";
    		        $output .= "<Name>" .htmlentities($name);
    		        $output .= "</Name>";
    		        $output .= "<Values>";
    		        $output .= "<Value>" .htmlentities($value)."</Value>";
    		        $output .= "</Values>";
    		        $output .= "</Field>";
    		}
  	$output .= "</Fields>";	
//  	$output .= "</Asset>";

//  This is commented out to debug.  	
//  	$file_name = $row["storyid"] .".xml";
//  	$fh=fopen($file_name,"w");
//  	fwrite($fh,utf8_encode($output));
//	fclose($fh);
//	chmod($file_name,0777);
		
}

$output .= "</Asset>";   	
 
 // print out XML that describes the schema
echo $output;

// close the connection
mysql_close($dbhandle);
Post Reply