View Full Version : PHP to XML
smudger
Apr 3rd 2008, 12:14 pm
Help !
i need a php script to query my database and produce an XML file with the following format
<?xml version="1.0" encoding="utf-8"?>
<gallery name="Gallery">
<picture name="picture1" description="test 1 " thumb="thumbs/1.jpg">images/1.jpg</picture>
<picture name="picture2" description="test xyz " thumb="thumbs/xyz.jpg">images/xyz.jpg</picture>
</gallery>
i will have a MySQL database holding the image names, decriptions and paths
has anybody done something like this before ?
thanks
Nigel
vishnups
Apr 3rd 2008, 12:22 pm
I have done it numerous times :)
Dade72
Apr 3rd 2008, 1:20 pm
you mean something like this?
function getXml() {
$this->connect();
$iRes = mysql_query($this->sSql);
if(!mysql_num_rows($iRes)) {
return "EMPTY";
} else {
$iNumFields = mysql_num_fields($iRes);
$iNumRes = mysql_num_rows($iRes);
$sRet = "<?xml version=\"1.0\"?>\n";
$sRet .= "<RESULTSET COUNT=\"$iNumRes\">\n";
while($iRow = mysql_fetch_array($iRes)) {
$sRet .= " <RESULT>\n";
for($a = 0; $a < $iNumFields; $a++) {
$sTmp = mysql_field_name($iRes,$a);
$sRet .= " ".strtoupper("<$sTmp>");
$sRet .= $iRow["$sTmp"];
$sRet .= strtoupper("</$sTmp>")."\n";
}
$sRet .= " </RESULT>\n";
}
$sRet .= "</RESULTSET>\n";
return $sRet;
}
}
smudger
Apr 4th 2008, 12:43 pm
yes, something like that ...
but can i use that exact code ?
smudger
Apr 5th 2008, 9:17 am
OK this is where i have got to now
it almost works .... but not quite ! ......
<?php
header("Content-type: text/xml");
$host = "localhost";
$user = "xyz";
$pass = "xyz";
$database = "xyz";
$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");
$query = "SELECT * FROM family ORDER BY family_name DESC";
$resultID = mysql_query($query, $linkID) or die("Data not found.");
$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<gallery name=\"Test Pictures\">\n";
for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
$row = mysql_fetch_assoc($resultID);
$xml_output .= "\t<picture name=\"NEEDS TO BE DYNAMIC\">\n";
$xml_output .= "\t\t<family>" . $row['family_name'] . "</family>\n";
// Escaping illegal characters
$row['text'] = str_replace("&", "&", $row['text']);
$row['text'] = str_replace("<", "<", $row['text']);
$row['text'] = str_replace(">", ">", $row['text']);
$row['text'] = str_replace("\"", """, $row['text']);
$xml_output .= "\t\t<text>" . $row['licence_details'] . "test</text>\n";
$xml_output .= "\t</picture>\n";
}
$xml_output .= "</gallery>";
echo $xml_output;
?>
the problem is ... i need to load the attribute 'name' from a value in my database - where i have marked "NEEDS TO BE DYNAMIC" in the script above
is this possible ?
can somebody please point me in the right direction ?
thanks
smudger
Apr 6th 2008, 5:37 am
can anybody help me further with this?
Dagon
Apr 6th 2008, 6:34 am
Not sure what you exactly need.
For example,
$xml_output .= "\t\t<family>" . $row['family_name'] . "</family>\n";
is already dynamic.
Just do the same for:
xml_output .= "\t<picture name=\"$row['picture_name']\">\n";
smudger
Apr 6th 2008, 7:22 am
Hi Dagon
yes thats what i figured as well
but when i have tried it, it gives me a parse error
Parse error: parse error, unexpected T_CONCAT_EQUAL in xml.php on line 21
Dagon
Apr 6th 2008, 7:27 am
Sorry, correct syntaxis is:
xml_output .= "\t<picture name=\"".$row['picture_name']."\">\n";
smudger
Apr 6th 2008, 7:42 am
Sorry ... still giving the same parse error
but i feel you are closer because the code is the correct colours in Dreamweaver !
Dagon
Apr 6th 2008, 7:53 am
I also forgot the $ in front ;)
$xml_output .= "\t<picture name=\"".$row['picture_name']."\">\n";
mm I think i should get some coffee :p
smudger
Apr 6th 2008, 9:36 am
excellent !
i didn't spot that either !
now it works !
thanks thanks , and thanks again !
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.