...

View Full Version : PHP and XML help



avatar3d
01-20-2010, 04:14 PM
Hello all,

Posted in this forum previously and got some good advice, stumbled across this problem. I am outputting XML which I got PHP to write to a different document called photos.xml however when I review the results it only displays the last entry in my database. I would like it to display all entries, and I'm not sure how to achieve this ..here is my code;




<?php

$link = mysql_connect("****","****","");
mysql_select_db("****");

$query = "SELECT ilink,image_name,description FROM gallery";

$results = mysql_query($query);


while($line = mysql_fetch_array($results)) {

$myFile = "photos.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "<?xml version=\"1.0\"?>\n";
fwrite($fh, $stringData);
$stringData = "<photos path=\"images/\">\n";
fwrite($fh, $stringData);
$stringData = "<photo name=\"".$line['image_name']."\" url=\"".$line['ilink']."\">".$line['description']."</photo>\n";
fwrite($fh, $stringData);
$stringData = "</photos>\n";
fwrite($fh, $stringData);
fclose($fh);
}
mysql_close($link);
?>



Thanks in advance :thumbsup:

angst
01-20-2010, 04:23 PM
try this;



<?php

$link = mysql_connect("****","****","");
mysql_select_db("****");

$query = "SELECT ilink,image_name,description FROM gallery";
$results = mysql_query($query);

$stringData = "<?xml version=\"1.0\"?>\n";

while($line = mysql_fetch_array($results)) {

$stringData .= "<photos path=\"images/\">\n";
$stringData .= "<photo name=\"".$line['image_name']."\" url=\"".$line['ilink']."\">".$line['description']."</photo>\n";
$stringData .= "</photos>\n";
}
mysql_close($link);

$filename = "photos.xml";
$handle = fopen($filename, 'a');
fwrite($handle, $stringData)
?>

JAY6390
01-20-2010, 04:29 PM
Your loop is writing to the file each iteration. You want the xml data and file opening to be outside the loop to begin with, and only the repeated section of the <photo> tags to be inside the loop. likewise the closing of the file needs to be outside the loop, after the } Something like this should work

<?php

$link = mysql_connect("****", "****", "");
mysql_select_db("****");

$query = "SELECT ilink,image_name,description FROM gallery";

$results = mysql_query($query);

$myFile = "photos.xml";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "<?xml version=\"1.0\"?>\n";
fwrite($fh, $stringData);
$stringData = "<photos path=\"images/\">\n";
fwrite($fh, $stringData);


while ($line = mysql_fetch_array($results)) {
$stringData = "<photo name=\"".$line['image_name']."\" url=\"".$line['ilink']."\">".$line['description']."</photo>\n";
fwrite($fh, $stringData);
}
$stringData = "</photos>\n";
fwrite($fh, $stringData);
fclose($fh);
mysql_close($link);

?>

avatar3d
01-20-2010, 04:33 PM
hello there thank you for the response,

It kind of worked however know it is doubling results and after one entry i get
<photos>
<photos>
<photos>

when there is only meant to be one

this is the error coming up in the browser;
Only one top level element is allowed in an XML document. Error processing resource 'http://localhost/xampp/work/

<photos path="images/">
-^

Thank you if you can help any further

angst
01-20-2010, 04:36 PM
ok, try this;


<?php

$link = mysql_connect("****","****","");
mysql_select_db("****");

$query = "SELECT ilink,image_name,description FROM gallery";
$results = mysql_query($query);

$stringData = "<?xml version=\"1.0\"?>\n";
$stringData .= "<photos path=\"images/\">\n";

while($line = mysql_fetch_array($results)) {
$stringData .= "<photo name=\"".$line['image_name']."\" url=\"".$line['ilink']."\">".$line['description']."</photo>\n";
}

$stringData .= "</photos>\n";

mysql_close($link);

$filename = "photos.xml";
$handle = fopen($filename, 'a');
fwrite($handle, $stringData)
?>

avatar3d
01-20-2010, 04:51 PM
Thank you for your help,

I think I have realised what the problem is my XML outputs like this


<?xml version="1.0"?>
<photos path="images/">
<photo name="valentino" url="gold.jpg">valentino in gold.</photo>
</photos>
<photos path="images/">
<photo name="dior4" url="self.jpg">dior new piece</photo>
</photos>


and im trying to get it to output in this way


<?xml version="1.0" encoding="utf-8"?>
<photos path="images/">
<photo name="Photo 1" url="1.jpg">photo description</photo>
<photo name="Photo 2" url="2.jpg">photo description</photo>
<photo name="Photo 3" url="3.jpg">photo description</photo>
<photo name="Photo 4" url="4.jpg">photo description</photo>
<photo name="Photo 5" url="5.jpg">photo description</photo>
<photo name="Photo 6" url="6.jpg">photo description</photo>
<photo name="Photo 7" url="7.jpg">photo description</photo>
<photos>


I think this is the reason it isn't working, thank you for your help so far not sure how to reform my code so it outputs like the above

JAY6390
01-20-2010, 04:53 PM
Try this method

<?php

// Create document with header version and encoding type
$xml = new DOMDocument('1.0', 'UTF-8');

// Create root node "photos" with path="images/" attribute
$photos = $xml->createElement('photos');
$photos->setAttribute('path', 'images/');

// Append the root element to the xml document header
$xml->appendChild($photos);

// MySQL connection and querying
mysql_connect('localhost', 'username', 'password');
myqsl_select_db('database');
$query = "SELECT ilink,image_name,description FROM gallery";
$results = mysql_query($query);

// Loop through each result
while($data = mysql_fetch_object($results)) {

// Create a new "photo" element with the description inside it
$photo = $xml->createElement('photo', $data->description);

// Append name and url attributes
$photo->setAttribute('name', $data->image_name);
$photo->setAttribute('url', $data->ilink);
//Append the data to the <photos> tag
$photos->appendChild($photo);
}

// Format the output nicely (indented)
$xml->formatOutput = true;

// Write the contents to photos.xml
file_put_contents('photos.xml', $xml->saveXML());It uses the DOM to create the document

avatar3d
01-20-2010, 05:01 PM
Thanks so much your amazing!!!!

JAY6390
01-20-2010, 05:05 PM
No problem. I think you missed my first post, that would do the same thing, but in the format you used



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum