Jump to content

Save executed php to file!


me1000

Recommended Posts

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
Share on other sites

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
Share on other sites

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
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.