jaiffed Posted October 14, 2011 Share Posted October 14, 2011 I have this code header("Content-type: text/xml"); $database = "rprice"; $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 plist ORDER BY date DESC"; $resultID = mysql_query($query, $linkID) or die("Data not found."); $xml_output = "<?xml version=\"1.0\"?>\n"; $xml_output .= "<graph>\n"; for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ $row = mysql_fetch_assoc($resultID); $xml_output .= "\t<set date =\"" . $row['date'] . "\" value =\"" . $row['sprice'] . "\">\n"; $xml_output .= "\t</set>\n"; } $xml_output .= "</graph>"; echo $xml_output; I want to save this xml file into the folder name Data what is the code of that Plz help Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/ Share on other sites More sharing options...
possien Posted October 14, 2011 Share Posted October 14, 2011 You already have your file content in the variable $xml_output. Do a search on php fwrite to write it to a file. Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279508 Share on other sites More sharing options...
jaiffed Posted October 15, 2011 Author Share Posted October 15, 2011 echo $xml_output .= saveXML(); $xml_output .= save("write.xml") i found this but its not working Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279560 Share on other sites More sharing options...
xyph Posted October 15, 2011 Share Posted October 15, 2011 Use SimpleXML. It was built for this. Comments explain what I'm doing. <?php // Set up our headers header( 'Content-type: text/xml; charset=UTF-8' ); // Bare bones XML to generate from. $xml_str = '<?xml version="1.0" ?><users></users>'; // Create a new instance of SimpleXML using the bones $xml = new SimpleXMLElement( $xml_str ); // Connect to database $db = new MySQLi( 'localhost','root','','db' ); // Grab all the users and the data we need $q = 'SELECT `id`,`name`,`email`,`realname` FROM `users`'; // Perform Query $r = $db->query( $q ); // Check if query failed. If so, echo empty XML file. Should log error. if( $r === FALSE ) die( $xml->asXML() ); // Loop through results while( $row = $r->fetch_assoc() ) { // Create a new child in '<users>' called '<user>'. Save this to // variable so we can give it arrtibutes and children $user = $xml->addChild( 'user' ); // Set attribute - '<user id="$id">' $user->addAttribute( 'id', $row['id'] ); // Give '<user>' children '<user id="$id"><name>$name</name></user>' etc. $user->addChild( 'name', $row['name'] ); $user->addChild( 'email', $row['email'] ); $user->addChild( 'realname', $row['realname'] ); } // Free up MySQL results $r->free(); // Echo our finished XML file. echo $xml->asXML(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279562 Share on other sites More sharing options...
jaiffed Posted October 15, 2011 Author Share Posted October 15, 2011 im having error in this $db = new MySQLi( 'localhost','root','12345','rlist' ); Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279563 Share on other sites More sharing options...
jaiffed Posted October 15, 2011 Author Share Posted October 15, 2011 i want xml file in ths format <graph> <set name='Jan' value='17400' hoverText='January'/> </graph> Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279564 Share on other sites More sharing options...
xyph Posted October 15, 2011 Share Posted October 15, 2011 i want xml file in ths format <graph> <set name='Jan' value='17400' hoverText='January'/> </graph> It's very easy to change my code to do that exactly. im having error in this $db = new MySQLi( 'localhost','root','12345','rlist' ); Then you've entered incorrect database information. That's my guess, without seeing the error. Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279568 Share on other sites More sharing options...
possien Posted October 15, 2011 Share Posted October 15, 2011 Are you trying to write to a database or a file? If file try this. Try this after your echo statement. $fhandle = fopen("data.xml", 'w') or die("can't create file"); fwrite($fhandle, $xml_output) or die("can't write to the file"); fclose($fhandle) This should write your date to the file data.xml. Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279569 Share on other sites More sharing options...
jaiffed Posted October 15, 2011 Author Share Posted October 15, 2011 yes i want to save as a .xml file & thanks u so much thiswork Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279570 Share on other sites More sharing options...
jaiffed Posted October 15, 2011 Author Share Posted October 15, 2011 everything is done after using possien code at the end the script $fhandle = fopen("data.xml", 'w') or die("can't create file"); fwrite($fhandle, $xml_output) or die("can't write to the file"); fclose($fhandle) ?> Its work but after that how could i open html file i put include 'dream.html'; & some other codes but its not working help me again guys Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279577 Share on other sites More sharing options...
possien Posted October 15, 2011 Share Posted October 15, 2011 I am not sure what you are asking? Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279620 Share on other sites More sharing options...
jaiffed Posted October 15, 2011 Author Share Posted October 15, 2011 after the end of script i want to call a HTML file called dream.html i used include ' dream.html'; but its gettin error so plz help me what is the code to call html file after the script processed. Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279625 Share on other sites More sharing options...
possien Posted October 15, 2011 Share Posted October 15, 2011 If you want to do it in php you might try this. Take the dream.html page and rename it to dream.php. In the page at the beginning create a variable say $dream and put all of the html in that variable. $dream= 'all of your html goes here.....'; In your main page use : require_once('dream.php'); echo $dream; Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279628 Share on other sites More sharing options...
jaiffed Posted October 15, 2011 Author Share Posted October 15, 2011 I have an dream.html file so i have to know how can i open this file after the end of this php file which create xml file. <?php // Set up our headers header( 'Content-type: text/xml; charset=UTF-8' ); // Bare bones XML to generate from. $xml_str = '<?xml version="1.0" ?><users></users>'; // Create a new instance of SimpleXML using the bones $xml = new SimpleXMLElement( $xml_str ); // Connect to database $db = new MySQLi( 'localhost','root','','db' ); // Grab all the users and the data we need $q = 'SELECT `id`,`name`,`email`,`realname` FROM `users`'; // Perform Query $r = $db->query( $q ); // Check if query failed. If so, echo empty XML file. Should log error. if( $r === FALSE ) die( $xml->asXML() ); // Loop through results while( $row = $r->fetch_assoc() ) { // Create a new child in '<users>' called '<user>'. Save this to // variable so we can give it arrtibutes and children $user = $xml->addChild( 'user' ); // Set attribute - '<user id="$id">' $user->addAttribute( 'id', $row['id'] ); // Give '<user>' children '<user id="$id"><name>$name</name></user>' etc. $user->addChild( 'name', $row['name'] ); $user->addChild( 'email', $row['email'] ); $user->addChild( 'realname', $row['realname'] ); } // Free up MySQL results $r->free(); // Echo our finished XML file. echo $xml->asXML(); $fhandle = fopen("data.xml", 'w') or die("can't create file"); fwrite($fhandle, $xml_output) or die("can't write to the file"); fclose($fhandle); ?> this file create the .xml file & save after that processing i want to open a html file which is dream.html i used include ' dream.html'; after the fclose($fhandle); but its not working getting errror Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279631 Share on other sites More sharing options...
possien Posted October 15, 2011 Share Posted October 15, 2011 Show me your dream.html Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279635 Share on other sites More sharing options...
possien Posted October 15, 2011 Share Posted October 15, 2011 I think I see what you are trying to do. Try this as your last line of code: header("Location: dream.html"); Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279636 Share on other sites More sharing options...
possien Posted October 15, 2011 Share Posted October 15, 2011 Try this as the last line of code: header("Location:dream.html"); Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279637 Share on other sites More sharing options...
jaiffed Posted October 15, 2011 Author Share Posted October 15, 2011 this is my file which make graph from saved .xml file change its ext .txt to .html [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279641 Share on other sites More sharing options...
jaiffed Posted October 15, 2011 Author Share Posted October 15, 2011 header("Location:dream.html"); not working... Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279644 Share on other sites More sharing options...
jaiffed Posted October 15, 2011 Author Share Posted October 15, 2011 thanx possien your worked header("Location: dream.html"); after remove the echo $xml_output; great person thnx a lot. Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279645 Share on other sites More sharing options...
possien Posted October 15, 2011 Share Posted October 15, 2011 Your welcome! Quote Link to comment https://forums.phpfreaks.com/topic/249149-save-xml-file-php-code/#findComment-1279646 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.