Jump to content

Recommended Posts

Hello,

 

Let me start from the top: I have a MYSQL database and have managed to generate an XML file using PHP (http://www.robmaps.co.uk/Floor_plans/Chadwick/links2.php). I have also got the necessary code which enables me to perfrom a 'live search' feature on my site, however, this code involves reading an .XML file, which I dont have. A snippet of PHP that reads the file:

 

$xmlDoc = new DOMDocument();
$xmlDoc->load("links2.xml");
$x=$xmlDoc->getElementsByTagName('link');

 

So I can generate XML string but I need to be able to have an dynamic XML file which will update when any  changes are made to the MYSQL database. How can i save the XML data to file? or What code can I use instead of the above to import the XML string? ???

 

Hope I've made it clear.

 

Thanks for your help, ;D

Rob

Link to comment
https://forums.phpfreaks.com/topic/152385-solved-save-as-xml-file/
Share on other sites

Thanks for the reply. I have heeded your advice.

 

OK, please bear with me, i'm new to PHP. So this is what I have now got:

<?php

require('databaseconnection.php');

// Select all the rows in the markers table
$query = "SELECT * FROM chadwickinfo WHERE 1";
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}

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

// Start XML file, echo parent node
echo '<pages>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  echo "<link>";
  echo "<title>" . $row["name"] . "</title>"; 
  echo "<url>" . $row["photo"] . "</url>"; 
  echo "</link>";
  
}

// End XML file
echo '</pages>';

$xmlfile = "links2.xml";
$file = fopen($xmlfile,"w");
fwrite($file, "<xml>");
fwrite($file, "<link><title>" . $row["name"] . "</title><url>" . $row["photo"] . "</url></link>");  
fwrite($file, "</xml>");
fclose($file);

?>

 

It still generates the XML string when previewed in the browser, however, I cant see any data added to the "links2.xml" file. Is that last bit of code correct?

 

Thanks,

Rob

 

ps I'm using Dreamweaver 8.0, is this sufficient? What is the best PHP program to use?

<?php

require('databaseconnection.php');

// Select all the rows in the markers table
$query = "SELECT * FROM chadwickinfo WHERE 1";
$result = mysql_query($query);
if (!is_resource($result)) {
  die('Invalid query: ' . mysql_error());
}

// header("Content-type: text/xml"); // not needed as we are just writing it to a file.

// Start XML file, echo parent node
$writeTo = '<pages>';

// Iterate through the rows, printing XML nodes for each
while ($row = mysql_fetch_assoc($result)){ // the @ is an error suppressor, I suggest not using it. Instead turn display_errors off in php.ini on your production server.
  // ADD TO XML DOCUMENT NODE
  $writeTo .= "<link>";
  $writeTo .= "<title>" . $row["name"] . "</title>"; 
  $writeTo .= "<url>" . $row["photo"] . "</url>"; 
  $writeTo .= "</link>";
  
}

// End XML file
$writeTo .= '</pages>';

$xmlfile = "links2.xml";
$file = fopen($xmlfile,"w+");

fwrite($file, "<xml>" . $writeTo . "</xml>");

fclose($file);

echo "File was written.";
?>

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.