how to create static page with php ?

Discussion in 'PHP' started by mohajer, Aug 4, 2007.

  1. #1
    Hello ,
    I build a news system with PHP , Mysql Database ,
    how to create static pages?
    for example we create static version of index.php?newsid=20 and Convert it To : new20.html when we create entry. ( static file No htaccess )
    I Want to collect information in Template And Then Create static file.
    for example in dynamic page :

    <html>
    <head> <title> Dynamic Format </title>
    <body>
    <?
    
    while($row = mysql_fetch_array($get)){
       $id    = $row[id];
        $sitename = $row[sitename];
       $sitecat = $row[cat];
    echo $id;
    echo $sitename;
    echo $sitecat;
    ?>
    </body>
    </html>
    PHP:

    now i want create this page with relevant information statically for each id.

    Any Article / Tutorial ?

    Regards.
     
    mohajer, Aug 4, 2007 IP
  2. Doskono

    Doskono Peon

    Messages:
    153
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Why do it with the html extention whats wrong with php extentions and get statements?
     
    Doskono, Aug 4, 2007 IP
  3. mohajer

    mohajer Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well ,
    I dont have any problem with php extentions or html extentions.

    I Just want to create Static page for each Entry .html or .php .
    Then Static Page dont have any related things with Database.

    I Want to collect Information then create static page with Collected information.
     
    mohajer, Aug 4, 2007 IP
  4. ErectADirectory

    ErectADirectory Guest

    Messages:
    656
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I do something similar to archive requests. Try buffering the output.

    
    <? ob_start(); ?>
    
    <html>
    <head> <title> Dynamic Format </title>
    <body>
    <?
    
    while($row = mysql_fetch_array($get)){
       $id    = $row[id];
        $sitename = $row[sitename];
       $sitecat = $row[cat];
    echo $id;
    echo $sitename;
    echo $sitecat;
    ?>
    </body>
    </html>
    
    <? 
    $out = ob_get_contents();
    ob_end_clean(); 
    
    $filename = "/path/to/directory/new" . $_GET['id'] . ".html" ;
    
    if (is_writable($filename)) {
      if (!$handle = fopen($filename, 'w+')) {
        echo "Cannot open file ($filename)";
        exit;
      }
    
      if (fwrite($handle, $out) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
      }
    
      fclose($handle);
    }
    
    echo $out;
    ?>
    PHP:
    I'm more of a problem solving programmer than an eloquent one (armed only with a roll of duct tape). Someone here will probably be able to help you with a more elegant solution.

    Basically what happens is that you save all the contents of the html page in a buffer [obstart()]. When the page has ended, you dump the buffer into a variable and save it on your server as an html file. After saving (or failure to save) spit out the content (echo $out ;) so it gets displayed on the page. I guess you could do a redirect to your newly created html page if you wanted.

    Remember to set your file permissions correctly or you will not be able to write to the desired folder.
     
    ErectADirectory, Aug 5, 2007 IP
  5. mohajer

    mohajer Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thank you ErectADirectory i will try that.
     
    mohajer, Aug 27, 2007 IP