Jump to content

Creating XML file using PHP, problems with Firefox


jammydevil

Recommended Posts

Hello,

I have a news PHP script using data from a MySQL database. I've used the below PHP script to extract the most recent three entries and create an XML file. This is then used by JavaScript to display the data in a rotating news display, similar to that found at [url=http://www.mlsnet.com]http://www.mlsnet.com[/url].

[code]include('news/flash/admin/functions.php');
connect('username','password','hurri_news','localhost');
$query = "SELECT newsid, image, title, home, DATE_FORMAT(date, '%M %d, %Y') as date FROM news_posts ORDER BY newsid DESC LIMIT 3";

$result = mysql_query($query);

// create a new XML document
$doc = new DomDocument('1.0');

// create root node
$root = $doc->createElement('root');
$root = $doc->appendChild($root);

// process one row at a time
while($row = mysql_fetch_array($result)) {
// add node for each row
$occ = $doc->createElement("news");
$occ = $root->appendChild($occ);

  // add a child node for each field
$child = $doc->createAttribute("id");
$child = $occ->appendChild($child);
$value = $doc->createTextNode(stripslashes2($row['newsid']));
$value = $child->appendChild($value);

$child = $doc->createAttribute("img");
$child = $occ->appendChild($child);
$value = $doc->createTextNode(stripslashes2($row['image']));
$value = $child->appendChild($value);

$child = $doc->createAttribute("title");
$child = $occ->appendChild($child);
$value = $doc->createTextNode(stripslashes2($row['title']));
$value = $child->appendChild($value);

$child = $doc->createAttribute("body");
$child = $occ->appendChild($child);
$value = $doc->createTextNode(stripslashes2($row['home']));
$value = $child->appendChild($value);
} // while

// get completed xml document
$xml_string = $doc->saveXML();

echo $xml_string;[/code]

I have managed to create the XML file and the script works perfectly in Internet Explorer 7 but does not work at all in Firefox. When I view the source in Firefox the data is displayed properly but JavaScript can't seem to use the data correctly. The JavaScript works fine when using a static XML file in both browsers but that would defeat the purpose of using the database. Can anybody advise me the best way to approach this problem or why Firefox would have trouble displaying this data? If this is the wrong forum I apologise.

Thanks,
Jay
Link to comment
Share on other sites

Never mind, I've fixed the problem. For anybody wanting to know, I hadn't set the headers correctly. I needed to add

[code]header("Content-Type: text/xml");[/code]

after the DomDocument is created. This allows Firefox to read the file as an XML file where as Internet Explorer isn't as picky.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.