TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Retrieve data error...plz help (http://www.talkphp.com/advanced-php-programming/4890-retrieve-data-error-plz-help.html)

cjay 08-23-2009 06:57 AM

Retrieve data error...plz help
 
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 Code:

<?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:

PHP Code:

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

adamdecaf 08-23-2009 12:49 PM

PHP Code:

<?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</gallery>\n";
fwrite($fh$stringData);

fclose($fh);

}
?>


JaimePinheiro 08-24-2009 06:55 PM

Try to replace this before while loop:
$myFile = "data.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "<?xml version=\"1.0\" encoding=\"ISO-8859-7\" ?>\n\n";

And this:
fclose($fh);
just after.

You are looping through each register of database, and for each one, you are openning the file, writing xml header, writing one register and closing the file, n times.

JaimePinheiro 08-24-2009 06:57 PM

Oh, just one more thing, the $stringData must concatenate the strings, so inside the while loop you'll have:
$stringData .= "<gallery>\n\n<photos>\n";
...


All times are GMT. The time now is 05:26 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0