Jump to content

How to call a php program from another php program and save the output


cranberry13

Recommended Posts

I want to be able to have a php script call another php script and have the data that the latter script creates written to a file. How does one do this? I can open a file and then write to the file using fwrite, however, I'm finding this to be a little tedious as there is lots of php and html intertwined. I really just want to run the script which calls the DB and sets up a webpage and then just have the output written to a .html file so that the webpage is static. The page only changes once a week and I don't want so many requests to the DB just to display the output. Just a note that I'm new to php.

 

thanks!

Here is what I did for this at one time. The page I wanted included I created a function to get my results then put everything into a variable. The in my main file I assigned the output of the function to a variable to write to my new page. Here is the example.

 

Page that you want results from.

<?php
require('config.php');
include('includes/mysql.php');
function mysqlresults($regID){
$print = "";
// $print .= Headers
$print .= "<table width=\"500\" align=\"center\" cellspacing=0 cellpadding=0>
  <tr bgcolor=\"F98F5B\">
    <td width=300 align=center>ITEM</td>
    <td width=100 align=center>QTY_REQ</td>
    <td width=100 align=center>STILL_NEEDS</td>
  </tr>
</table>\n";
// Set initial group values
$lastcat = "";
// Query database
$sql = "SELECT * FROM ". $regID ." ";
$sql .= "GROUP BY CATAGORY, ITEM";
  $res = mysql_query($sql);
  $num_rows = mysql_num_rows($res);
if($num_rows > 1){
// Set initial row color
$bgcolor = "FFFFFF";
    while ($rows = mysql_fetch_assoc($res)){
// $print .= Group Total
if ($rows["CATAGORY"] != $lastcat) {
// $print .= Group Name
$print .= "<table width=\"500\" align=\"center\" cellspacing=0 cellpadding=0>
  <tr bgcolor=83EA44>
    <td colspan=3 align=left><strong>Catagory: ".$rows["CATAGORY"]."</strong></td>
  </tr>
</table>\n";
}
// Alternate row color
if ($bgcolor == "#E0E0E0"){
  $bgcolor = "#FFFFFF";
} else {
  $bgcolor = "#E0E0E0";
}
// $print .= Database Details
$print .= "<table width=500 align=center cellspacing=0>
  <tr bgcolor=".$bgcolor.">
    <td width=300 align=center>".$rows["ITEM"]."</td>
    <td width=100 align=center>".$rows["QTY_REQ"]."</td>
    <td width=100 align=center>".$rows["STILL_NEEDS"]."</td>
  </tr>
</table>\n";
// Reset group values
$lastcat = $rows["CATAGORY"];
}
} else {
// $print .= No data message
$print .= "<table width=650 align=center>
  <tr>
    <td align=center><strong>NO ITEMS!!</strong></td>
  </tr>
</table>\n";
}
return $print;
}
?>

 

Page which gets the results and writed the new page

<?php
include('incsample.php');
$newpage = mysqlresults("items");
$filename = "newhtmlpage.html";
$handle = fopen($filename, "w");
    if (fwrite($handle, $newpage) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }
?>

 

Substitute what you need

 

Ray

 

Archived

This topic is now archived and is closed to further replies.

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