1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development html generating php files - solved!

Discussion in 'Software' started by OneSeventeen, 8 Jul 2003.

  1. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    I'm trying to create dynamic pages with php/mysql and decided to make it easy on my server by having a php file generate html files based on the database. I'm doing this by having the normal dynamic .php files read into $srcfile using a temporary file $tmp and saving the final filename $target

    So after setting $srcfile='http://localhost/mysrc.php'
    and $tmp = 'tmp.htm'
    and $target='page.htm'
    I run:
    PHP:
    <?php
    @unlink($tmp);
    $dp=fopen($srcfile'r');
    $data=fread($dp1024*1024);
    fclose($dp);
    $tmpfile=fopen($tmp'w');
    fwrite($tmpfile$data);
    fclose($tempfile);
    $ok=copy($tmpfile$target);
    unlink($tmpfile);
    ?>
    When I access the page and it runs through everything (I have it print some more html after the php section to show it completed the script) I go to the new html page.. page.htm in this example, and it doesn't have any of the php generated stuff, just the html outside of the <?php ?>.

    If I redo this a few times it will work every once in a while.
    I added an echo before any of the php in mysrc.php and it worked fine, but I took it off and it won't work anymore.

    It just confuses me that I can do the exact same thing 10 times and get 10 different results.

    Ideas? Better ways of doing this?
    (btw, I am using error reporting in my regular script, but I took it out for space reasons)
     
  2. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    Well, I found out one of the problems: I was using Internet Explorer.

    I am usually an IE fan, but I didn't realize the refresh button didn't refresh the page. Not sure what it does actually...

    It decided 'hey, I've seen this php page before! I'll just save him the trouble of loading it and show him what it looked like the last time he viewed it!' Therefore NOT envoking the php code. I still don't get why it takes three or four refreshes in Mozilla though... although it will eventually work.

    Is there a way I can add file-writing capabilities to the source file itself? So it grabs the info from the database and throws it on a static .htm file?

    Would
    $data = "So then this bumbling moron said, \"";
    $data = $data . "blah de blah blah blah\"";
    echo($data);
    produce:
    So then this bumbling moron said, "blah de blah blah blah"
    ?
     
  3. markit

    markit What's a Dremel?

    Joined:
    25 May 2002
    Posts:
    207
    Likes Received:
    0
    hold ctrl and then press refresh and 'refresh' should refresh the page and not use cache
     
  4. markit

    markit What's a Dremel?

    Joined:
    25 May 2002
    Posts:
    207
    Likes Received:
    0
    $data = $data . "blah de blah blah blah\"";

    should be

    $data = 'So then this bumbling moron said, \"';
    $data .= 'blah de blah blah blah\"';

    otherwise at the end it closes then opens again. using different quotes makes php understand the difference
     
  5. linear

    linear Minimodder

    Joined:
    5 Oct 2001
    Posts:
    4,393
    Likes Received:
    1
  6. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    So would I instead write:
    PHP:
    <?php
    @unlink($tmp);
    $dp=eval($srcfile);
    $data=fread($dp1024*1024);
    fclose($dp);
    $tmpfile=fopen($tmp'w');
    fwrite($tmpfile$data);
    fclose($tempfile);
    $ok=copy($tmpfile$target);
    unlink($tmpfile);
    ?>
    Sorry, I'm still learning how to RT*M.
     
  7. linear

    linear Minimodder

    Joined:
    5 Oct 2001
    Posts:
    4,393
    Likes Received:
    1
    I think you are confusing your file pointers and the string representing the page.

    eval($data) seems like the thing, ifI read your code correctly.

    Code:
    //set a buncha vars
    $foo = "3-dimensional";
    $bar = "alligators";
    
    // read your template file into a string (don't mess with the low level operations, d00d)
    $mytemplate = [url=http://us4.php.net/manual/en/function.file-get-contents.php]file_get_contents[/url](template.php);
    
    //then eval
    $outfile = eval($mytemplate);
    
    //and barf it out
    echo $outfile;
    The above is more or less how I'd go after it.
    refernces to $foo and $bar get expanded, as do any calculations based on them. You may want to look into output buffering as well, but get the basics down first, eh?
     
  8. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    Here's what I ended with:
    PHP:
    $filename 'authors2.htm';
    $srcfile='url to template php file'
    $htmldata file_get_contents($srcfile);
    $dynfile=fopen($filename'w');
    fwrite($dynfile$htmldata);
    fclose($dynfile);
    It works like a charm!
    For some reason it refused to eval(); my stuff... I wish I had written the info down, but I guess I didn't need it in this instance.
    I just can't wait for php 5 where I get to file_put_contents();!

    (I figured I should wait for a bit more php experience before beta testing it on my business' web server :p)
     
  9. exile

    exile What's a Dremel?

    Joined:
    8 Mar 2004
    Posts:
    46
    Likes Received:
    0
    REFRESHING IE:
    When you press REFRESH or F5, and it dosent refresh try SHIFT+REFRESH or CTRL+REFRESH. I dont remember which one. I was just told about it once.
     
  10. MechWarrior

    MechWarrior What's a Dremel?

    Joined:
    13 Oct 2003
    Posts:
    55
    Likes Received:
    0
    All my php pages output a no-cache header. Always. 'nuff said.

    Code:
    		// Print the no-cache header
    		
    		@header("Cache-Control: no-cache, must-revalidate");
    		@header("Pragma: no-cache");
     

Share This Page