SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Saving parsed PHP files as HTML files

    Hello,

    I have a script running on my local machine that reads in data from a database, stores it an object. The class code for creating each object is below:

    PHP Code:
    class Announcement {
        
    //DECLARE VARIABLES
        
    var $spnsr_id//sponsor ID
        
    var $spnsr_name//sponsor name
        
    var $pgm_name//program name
        
    var $pgm_description//program description
        
    var $pgm_contact//program contact
        
    var $cmp_guideline_url//competition URL
        
    var $cmp_name//competition name
        
    var $cmp_awrd_range//value of the award
        
    var $cmp_open_dt//is it an open-ended date?
        
    var $cmp_internal_dt//internal deadline
        
    var $cmp_dean_dt//dean's deadline
        
    var $cmp_agency_dt//agency deadline
        
    var $cmp_detail//compeition description
        
    var $cmp_admin_info//application details
        
    var $cmp_int_contact_nm//RDS contact person's name
        
    var $interest_descr = array(); //array of keywords
        
    var $address = array(array()); //2D array of addresses
        
        //CONSTRUCTOR
        
    function Announcement($spnsr_id$spnsr_name$pgm_name$pgm_description$pgm_contact$cmp_guideline_url$cmp_name$cmp_awrd_range$cmp_open_dt$cmp_internal_dt$cmp_dean_dt$cmp_agency_dt$cmp_detail$cmp_admin_info$cmp_int_contact_nm$interest_descr$address) {
            
    $this->spnsr_id $spnsr_id;
            
    $this->spnsr_name $spnsr_name;
            
    $this->pgm_name $pgm_name;
            
    $this->pgm_description $pgm_description;
            
    $this->pgm_contact $pgm_contact;
            
    $this->cmp_guideline_url $cmp_guideline_url;
            
    $this->cmp_name $cmp_name;
            
    $this->cmp_awrd_range $cmp_awrd_range;
            
    $this->cmp_open_dt $cmp_open_dt
            
    $this->cmp_internal_dt $cmp_internal_dt
            
    $this->cmp_dean_dt $cmp_dean_dt
            
    $this->cmp_agency_dt $cmp_agency_dt
            
    $this->cmp_detail $cmp_detail
            
    $this->cmp_admin_info $cmp_admin_info
            
    $this->cmp_int_contact_nm $cmp_int_contact_nm
            
    $this->interest_descr $interest_descr
            
    $this->address $address;
        }
        
        
        
    //GENERATES THE ANNOUNCEMENT USING THE TEMPLATE
        
    function generateAnnouncement() {
            require(
    "../templates/tmpAnnouncement.php");
        }

    What I want to do is save the result (output) of ../templates/tmpAnnouncement.php to an html file. That is, instead of displaying the output I want to save it. Any ideas? Here is tmpAnnouncement.php:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>RDS Announcement</title>
    </head>
    <body>
    <center>
    <table width="650" border="2">
    <tr><td>
    <table width="650">
      <tr>
        <td width="160">
        <img src="../images/logo.gif" alt="Research Western" />
        </td>
        <td>
        <b>Funding Opportunites - Research</b><br />
        Further details regarding this announcement can be found at:<br />
        <a href="<?php echo (trim($this->cmp_guideline_url) != "" $this->cmp_guideline_url "N/A"); ?>" target="_blank"><?php echo (trim($this->cmp_guideline_url) != "" $this->cmp_guideline_url "N/A"); ?></a>
        </td>
      </tr>
      <tr>
        <td>
        <b>Agency/Award:</b>
        </td>
        <td>
        <?php echo (trim($this->spnsr_name) != "" $this->spnsr_name." - " "N/A"); ?> <?php echo (trim($this->pgm_name) != "" $this->pgm_name." - " "N/A"); ?> <?php echo (trim($this->cmp_name) != "" $this->cmp_name "N/A"); ?>
        </td>
      </tr>
      <tr>
        <td>
        <b>Website: </b>
        </td>
        <td>
        <a href="<?php echo (trim($this->cmp_guideline_url) != "" $this->cmp_guideline_url "N/A"); ?>" target="_blank"><?php echo $this->cmp_guideline_url?></a>
        </td>
      </tr>
      <tr>
          <td colspan="2">
        <hr />
        </td>
      </tr>
      <tr>
        <td>
        <b>Grant Amount:</b>
        </td>
        <td>
        <?php echo (trim($this->cmp_award_range) != "" $this->cmp_award_range "N/A"); ?>
        </td>
      </tr>
      <tr>
        <td>
        <b>Deadline:</b>
        </td>
        <td>
        Open Deadline? <?php echo (trim($this->cmp_open_dt) != "" $this->cmp_open_dt "N/A"); ?><br />
        Internal: <?php echo (trim($this->cmp_internal_dt) != "" $this->cmp_internal_dt "N/A"); ?><br />
        Dean's: <?php echo (trim($this->cmp_dean_dt) != "" $this->cmp_dean_dt "N/A"); ?><br />
        Agency: <?php echo (trim($this->cmp_agency_dt) != "" $this->cmp_agency_dt "N/A"); ?>
        </td>
      </tr>
      <tr>
          <td colspan="2">
        <hr />
        </td>
      </tr>
      <tr>
        <td>
        <b>Subject Areas:</b>
        </td>
        <td>
        <?php 
        
    for ($x 0$x count($this->interest_descr); $x++) {
            echo (
    trim($this->interest_descr[$x]) != "" $this->interest_descr[$x]." <br />" "");
        }
         
    ?>
        </td>
      </tr>
      <tr>
        <td>
        <b>Description:</b>
        </td>
        <td>
        <?php echo (trim($this->pgm_description) != "" $this->pgm_description "N/A"); ?><br />
        <?php echo (trim($this->cmp_detail) != "" $this->cmp_detail "N/A"); ?>
        </td>
      </tr>
      <tr>
          <td colspan="2">
        <hr />
        </td>
      </tr>
      <tr>
        <td>
        <b>Application Details:</b>
        </td>
        <td>
        <?php echo (trim($this->cmp_admin_info) != "" $this->cmp_admin_info "N/A"); ?>
        </td>
      </tr>
      <tr>
        <td colspan="2">
        <b>NOTE:</b> Letters of Intent and Full Applications are to be processed through Research Development Services for institutional signatures, and are to be accompanied by a completed RDAF form (bearing applicant, Chair and Dean signatures).  Requests for Institutional signatures should be received no less than 3 days prior to the posted Agency deadline.
        </td>
      </tr>
      <tr>
        <td>
        <b>Western Contacts:</b>
        </td>
        <td>
        <?php echo (trim($this->cmp_int_contact_nm) != "" $this->cmp_int_contact_nm "N/A"); ?>
        </td>
      </tr>
      <tr>
          <td colspan="2">
        <hr />
        </td>
      </tr>
      <tr>
        <td>
        <b>Agency Contacts:</b>
        </td>
        <td>
        <?php echo (trim($this->pgm_contact) != "" $this->pgm_contact "N/A"); ?><br />
        <?php
            
    //the array of addresses
            
    for ($x 0$x count($this->address); $x++) {
                echo (
    trim($this->address[$this->numAddresses][spnsr_addr_descr]) != "" $this->address[$this->numAddresses][spnsr_addr_descr]." <br />" ""); 
                echo (
    trim($this->address[$this->numAddresses][spnsr_addr1]) != "" $this->address[$this->numAddresses][spnsr_addr1]." <br />" ""); 
                echo (
    trim($this->address[$this->numAddresses][spnsr_addr2]) != "" $this->address[$this->numAddresses][spnsr_addr2]." <br />" ""); 
                echo (
    trim($this->address[$this->numAddresses][spnsr_addr3]) != "" $this->address[$this->numAddresses][spnsr_addr3]." <br />" ""); 
                echo (
    trim($this->address[$this->numAddresses][spnsr_addr4]) != "" $this->address[$this->numAddresses][spnsr_addr4]." <br />" "");
                echo (
    trim($this->address[$this->numAddresses][spnsr_city]) != "" $this->address[$this->numAddresses][spnsr_city].", " "");
                echo (
    trim($this->address[$this->numAddresses][spnsr_state]) != "" $this->address[$this->numAddresses][spnsr_state].", " "");
                echo (
    trim($this->address[$this->numAddresses][spnsr_country_descr]) != "" $this->address[$this->numAddresses][spnsr_country_descr]." <br />" ""); 
                echo (
    trim($this->address[$this->numAddresses][spnsr_postal_cd]) != "" $this->address[$this->numAddresses][spnsr_postal_cd]." <br />" "");
                echo (
    trim($this->address[$this->numAddresses][spnsr_prefix]) != "" "Tel: ".$this->address[$this->numAddresses][spnsr_prefix]."-" "");
                echo (
    trim($this->address[$this->numAddresses][spnsr_phone]) != "" $this->address[$this->numAddresses][spnsr_phone]." " "");
                echo (
    trim($this->address[$this->numAddresses][spnsr_ext]) != "" "ext ".$this->address[$this->numAddresses][spnsr_ext]." <br />" "");
                echo (
    trim($this->address[$this->numAddresses][spnsr_fax]) != "" "Fax: ".$this->address[$this->numAddresses][spnsr_fax]." <br />" "");
            }
        
    ?>
        </td>
      </tr>
    </table>
    </td></tr>
    </table>
    </center>
    </body>
    </html>
    Ryan Price
    Subscribe to my blog for regular tips and tricks

  2. #2
    SitePoint Zealot Bill Palmer's Avatar
    Join Date
    Oct 2005
    Location
    London, UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have another script that fopen()s the script that does the database magic/includes the template, then write out the output to a file using fwrite().

  3. #3
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That doesn't work because of how everything is interconnected for me. I did find the solution though. PHP offers an output buffer that will save any output for you. Here is my solution, and its working perfectly:

    PHP Code:
        //GENERATES THE ANNOUNCEMENT USING THE TEMPLATE
        
    function generateAnnouncement() {
            
    ob_start(); //turn on output buffering
            
    require("../templates/tmpAnnouncement.php"); //parse the file
            
    $output ob_get_contents(); //save its contents
            
    ob_end_flush(); //output the buffer's contents and turn off buffering
            
            
    $outputFile fopen("../generatedAnnouncements/".$this->spnsr_id."-".$this->pgm_name."-".$this->cmp_name.".html""w");
            
    fwrite($outputFile$output);
                            
        } 
    Ryan Price
    Subscribe to my blog for regular tips and tricks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •