me1000 Posted April 1, 2007 Share Posted April 1, 2007 Hello, So Im loading an XML file within a page. I made it dynamic by coding it in PHP and adding a .htaccess so that it executes at a PHP file, well it executed just file, but it didnt work in the page that it was called upon by! so I just grabbed the code from the source (the xml created with the PHP) and saved it as a XML file. well that is fine for now, but not perfect. I already have the script that makes the XML file, but I want to be able to run it, and when i do it save the output of the PHP to the server as an xml file. here is the PHP code im using now, <?php //connect variables $dbhost = "localhost"; // database host $dbname = "databasename"; // database name $dbuser = "username"; // database username $dbpass = "password"; // database password $sTableName = "tablename"; // Your table name // The database connection mysql_connect("$dbhost", "$dbuser", "$dbpass"); mysql_select_db("$dbname"); // LET THE CODE BEGIN $sql = "SELECT ID, PAGE_TITLE ". //PAGE_TYPE "FROM $sTableName ORDER BY PAGE_TYPE ASC"; $sql_result = mysql_query($sql); echo "<pages> "; while ($row = mysql_fetch_array($sql_result)) { $page_id = $row["ID"]; $page_title = $row["PAGE_TITLE"]; //$page_type = $row["PAGE_TYPE"]; //$page_link = '<a href="index.php?page_id='.$page_id.'">'.$page_title.'</a>'; $page_link = 'index.php?page_id='.$page_id; echo "<page> "; echo "<name>".$page_title."</name> "; echo "<url>".$page_link."</url> "; echo "</page> "; } echo "</pages>"; ?> so when I goto a page, it runs the script and saves it as a xml file, and I dont have to think twice about it, but what would be really nice is to setup a cron job for it, but ill worry about the cron job later... Thank You, Link to comment https://forums.phpfreaks.com/topic/45150-save-executed-php-to-file/ Share on other sites More sharing options...
tippy_102 Posted April 1, 2007 Share Posted April 1, 2007 $file= fopen( $filename, "w"); $_xml "<name>".$page_title."</name>\n"; etc... etc... fwrite($file, $_xml); fclose($file); Link to comment https://forums.phpfreaks.com/topic/45150-save-executed-php-to-file/#findComment-219234 Share on other sites More sharing options...
me1000 Posted April 1, 2007 Author Share Posted April 1, 2007 Thank You, Problem is im not sure where to stick it all within the loop... this is what I did, $filename = "pages2.xml"; $file= fopen( $filename, "w"); fwrite($file, "<pages>"); while ($row = mysql_fetch_array($sql_result)) { $page_id = $row["ID"]; $page_title = $row["PAGE_TITLE"]; $page_link = 'index.php?page_id='.$page_id; $_xml= "<name>".$page_title."</name>\n <url>".$page_link."</url>\n"; } fwrite($file, "</pages>"); fwrite($file, $_xml); fclose($file); it comes out like this, <pages></pages><name>LAST page IN the DATABASE table</name> <url>index.php?page_id=62</url> so it only shows the last row of the database table, and the <page> tags are in the wrong spot! Im thinking it may have something to do with the "w" in the fopen command, Thank You, Link to comment https://forums.phpfreaks.com/topic/45150-save-executed-php-to-file/#findComment-219276 Share on other sites More sharing options...
me1000 Posted April 1, 2007 Author Share Posted April 1, 2007 here is my finished code, is there any way to clean it up some? thanks for your help! $filename = "pages2.xml"; $file= fopen( $filename, "w"); $opentag="<pages>\n"; fwrite($file, $opentag); fclose($file); while ($row = mysql_fetch_array($sql_result)) { $page_id = $row["ID"]; $page_title = $row["PAGE_TITLE"]; $page_link = 'index.php?page_id='.$page_id; $_xml= "<page> <name>".$page_title."</name> <url>".$page_link."</url> </page>"; $file= fopen( $filename, "a"); fwrite($file, $_xml); fclose($file); } $file= fopen( $filename, "a"); $closetag="</pages>"; fwrite($file, $closetag); fclose($file); Link to comment https://forums.phpfreaks.com/topic/45150-save-executed-php-to-file/#findComment-219297 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.