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
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; } }
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
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";
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
Sorry ... still giving the same parse error but i feel you are closer because the code is the correct colours in Dreamweaver !
I also forgot the $ in front $xml_output .= "\t<picture name=\"".$row['picture_name']."\">\n"; mm I think i should get some coffee