PHP, Array, and XML tree
Posted: Tue Jul 17, 2007 11:08 am
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);
?>