Monday, April 25, 2011

PHP: re-parse entire page before serving?

At the end of a page, if something occurs, it needs to be cleared, then the entire page needs to be re-parsed before serving to the client. I was going to echo out a javascript to refresh the page, but that will make them load the page and then reload it...I was wondering if there was a way to just tell the php engine to go back to the beginning and re-parse the entire page?

Thanks!

I will try to explain the problem more clearly but it is complicated and I am a terrible communicator. I on the page that lists products I am giving users the option to select fields to narrow the results. The system remembers this so they don't have to keep selected them. If they narrow a category like metal color and then go to a category that metal color is irrelevant like crystal figurines it will not show any results because none will match the metal color chosen. To generate the query to pull the products from the data-base is very complicated because different categories have different requirements to find the correct products. so once the query is generated I want to test it against mysql_num_rows() and if there is no results clear out the users choices and start over.

From stackoverflow
  • You're being a little vague, but if you're merely talking about reparsing the output, you could do that using output buffering.

    John Isaacks : Can you please explain how to use output buffering? I am completely new to the concept. Thanks!
    Mike B : http://us.php.net/manual/en/function.ob-start.php
    Trevor Bramble : Instead of PHP giving the web server output as it's parsed, it is held in a buffer until you explicitly release it. Please see the included link.
  • I'm not entirely clear what the issue is, but couldn't you decide what is to be shown before creating the HTML, and then send the right thing the first time?

  • Output buffering (ob_start and ob_clean), combined with separating the functionality at hand into a separate file and eval()'ing that should do the trick.

    Oh, and recent PHP versions actually have a goto statement... although I'll always deny mentioning anything about it. :-)

  • I think you're going about it a little bit off.

    What you should do to reparse the page is to redirect the user to the page again, using

    header('Location: thepagefile.php');
    

    however if you actually would like to reparse the file without creating a new page, you could also just include the file again:

    include thepagefile.php

    But you'd probably get the same result. If you want to actually parse the output of the page you'd do something like:

    ob_start(); // this is at the very top of the code/page
    // all the code goes here
    $output = ob_get_clean();
    eval($output); // WTF?
    

    but that actually makes no sense, but I hope it helps.

    I'd actually like to know what the real problem you're trying to solve really is.

  • To generate the query to pull the products from the data-base is very complicated because different categories have different requirements to find the correct products. so once the query is generated I want to test it against mysql_num_rows() and if there is no results clear out the users choices and start over.

    In that case, just put the query inside a function that returns the result, check the row count, and if it's zero clear the filters and call that function a second time.

  • I think your looking for something like this:

    <?php
    ob_start(); //we start output buffering, this means nothing is send to the browser
    //We do some code stuff
    $time = microtime();
    echo "Hai \n"; //Note taht mixing logic and output in real life
    echo $time;  // is terribly practice
    echo "\n bai"; //I do it here purely for the example
    if(/*some condition */){
     $anErrorHappened = true;
    }
    if($anEroorHappened === true){
      //Load the output in a var if you need it
      //Otherwise don't
      $output = ob_get_clean();
      //Do other code stuff
      //I.E. send an error page
      include('errorPage.html');
    }
    else{
     ob_end_flush(); //Send everything the script has echo()'d, print()'ed and send to the browser in any other way (I.E. readfile(), header() etc.)
    }
    ?>
    

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.