|  | Posted by NC on 06/12/06 11:41 
Simon wrote:>
 > As we all know, JavaScript is client side and php is server side, (the php
 > code is 'allowed' to do stuff on the server that JavaScript cannot).
 > The problem with php is that it timeout after a while, (and the user also
 > has no clue as to what is going on for a long time).
 > I need to run a script on the server that could take a very long time.
 
 Does the user need to see anything based on the results of this long
 process?
 
 > How could I achieve something like that?
 
 There are two options I can think of; neither involves JavaScript.  I
 am sure there are many other options, too.
 
 1. Use command-line scripting
 
 If there is no need for the user to see the output, you can start your
 processing script in the background using the command-line interpreter.
 On Unix, this will look something like this:
 
 exec('php my_very_long_script.php &');
 
 2. Use "piecemeal" processing
 
 Say, you need to process an unknown number of records in a database.
 You estimated that processing 100 records at a time is done quickly
 enough not to trigger the execution time limit.  So you can repeatedly
 run the same script to process a new group of records each time;
 something like this:
 
 if (isset($_GET['last_processed'])) {
 $last = (int) $_GET['last_processed'];
 $query = "SELECT * FROM mytable WHERE id>$last ORDER BY id LIMIT
 100";
 } else {
 $query = "SELECT * FROM mytable ORDER BY id LIMIT 100";
 }
 $result = mysql_query($query);
 while ($record = mysql_fetch_array($result)) {
 // Process your records
 $last_processed = $record['id'];
 }
 if (mysql_num_rows($result) == 100) {
 header('Location: ' . $_SERVER['PHP_SELF'] .
 "?last_processed=$last_processed");
 echo "$last_processed records processed so far... Please wait...";
 die();
 }
 echo "Finished processing; $last_processed records were processed.";
 
 Cheers,
 NC
  Navigation: [Reply to this message] |