Jump to content

PHP, Array, and write to file


nmpku

Recommended Posts

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);

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.