PDA

View Full Version : XML output via PHP



lbeetles
October 3rd, 2004, 08:57 PM
I hope someone can help me out, with this.

I am getting data from a MySql database via php and outputting it into xml.

Here is my code:

<?

include("connect.php");

header("Content-type: text/xml");

$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 head_entry, text_entry, DATE_FORMAT(add_date, '%d %b %Y, %H:%i') as vtime FROM newsbox WHERE active = 'y' ORDER BY add_date DESC";
$result = mysql_query($query, $linkID) or die("Data not found.");

$xml_output = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
$xml_output .= "<menu>\n";

for ($x = 0; $x < mysql_num_rows($result) ; $x++){
$row = mysql_fetch_assoc($result);

$xml_output .= "\t<item head =\"" . "&lt;b&gt;" . $row['head_entry'] . "&lt;/b&gt;" . "\" entry =\"" . $row['text_entry'] . "\" date =\"" . $row['vtime'] . "\" >\n";

$xml_output .= "\t</item>\n";
}

$xml_output .= "</menu>";

print $xml_output;

?>
My problem is that i need to have links, bold and italic in there. How would i get php to detect <b></b>, <i></i>, <a href=></a> and change it so xml can understand it and output the result correctly.

Would i have to use CDATA and if so, how???

Cheers

Lee

[m]
October 3rd, 2004, 09:20 PM
You want html in xml? Use CDATA tags.

And it's not smart to put an header declaration befor you do anything. What if an error pops up? It would not validate as xml, and that means a lot of nastyness.

lbeetles
October 4th, 2004, 05:31 AM
[m] yeah thats exactly what i need, html tags in xml.

How would i do this with the script i am already using???

Also what do u suggest i should do with the header so if i do get an error the xml would validate???