cjay Posted August 23, 2009 Share Posted August 23, 2009 hello ppl! i have a xml file and i refresh it with a php file... into the php file i have a query which i retrieve my data from a sql db...the output result is n't the right...because it "write" to the xml file only the last record from db...look what i mean: php file: <?php @include("xconf.php"); $query = "SELECT * FROM images"; $result = mysql_query($query); while($oz = mysql_fetch_array($result)) { $myFile = "data.xml"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "<?xml version=\"1.0\" encoding=\"ISO-8859-7\" ?>\n\n<gallery>\n\n<photos>\n"; fwrite($fh, $stringData); $stringData = "<photo>\n<thumb src=\"$oz[mikro]\" />\n<img src=\"$oz[s]\" />\n<caption text=\"$oz[title]\" />\n<desc> <![CDATA[$oz[per]]]> </desc>\n</photo>\n\n"; fwrite($fh, $stringData); $stringData = "</photos>\n\n"; fwrite($fh, $stringData); $stringData = "</gallery>\n"; fwrite($fh, $stringData); fclose($fh); } ?> and the data.xml: <?xml version="1.0" encoding="ISO-8859-7" ?> <gallery> <photos> <photo> <thumb src="http://www.talkphp.com/images/thumb_Free_web_development_icon_set.jpg" /> <img src="http://www.talkphp.com/images/Free_web_development_icon_set.jpg" /> <caption text="yt4" /> <desc> <![CDATA[er]]> </desc> </photo> </photos> </gallery> how can i correct the code? i want the output result appear all the db records... thank you friendly Quote Link to comment Share on other sites More sharing options...
corbin Posted August 23, 2009 Share Posted August 23, 2009 $fh = fopen($myFile, 'w') or die("can't open file"); You're reopening the file handle every time the loop runs. Just so happens that opening for writing (w) truncates the file if it exists. So you're overwriting the file every time you reopen it. (In other words, put the $fh = .... outside of the while loop. Also, put the fclose outside of the loop.) And an unrelated suggestion as to coding style: @include("xconf.php"); Shouldn't have the @. Error suppression is typically bad (unless for some weird reason it's ok if an error occurs). If you're worried about users seeing the error in a live environment, set display_errors to 0. Error suppression makes for slower code and confusion debugging if something does go wrong. Also, technically include("xconf.php"); could be (and some would say should be) include "xconf.php"; since include is a language construct, not a function (although it really doesn't make much of a difference in this situation). 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.