nmpku Posted July 17, 2007 Share Posted July 17, 2007 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 // 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); Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 17, 2007 Share Posted July 17, 2007 i am having trouble doing this. What trouble exactly? What does the script do so far? It looks to me like you're not writing to the file as yet - does that mean you are unsure how to do that, or does that mean that you debugging prior to that by echoing the data(that appears to be what you are doing at the moment) Quote Link to comment Share on other sites More sharing options...
nmpku Posted July 17, 2007 Author Share Posted July 17, 2007 my previous code was just echoing to debug...now I got that working and the code below is where i am writing the file. However I am getting "HTTP Wrapper" errors, as well as FTP errors. Please take a look and see if someone you can help: 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@ftpserveraddress/".$row["storyid"] .".xml","w"); fwrite($fh,utf8_encode($output)); fclose($fh); } mysql_close($dbhandle); ?> Quote Link to comment Share on other sites More sharing options...
nmpku Posted July 18, 2007 Author Share Posted July 18, 2007 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." 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: // 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); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.