Jump to content


Pausing PHP Script to update display

  • Please log in to reply
6 replies to this topic

#1 lonewolf277

  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 01 June 2006 - 01:42 PM

This is my first post, so I'll try make it a good one [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]

Is it possible to stop a PHP script in order to update a status display, then finally resume the script? I'm creating a mailing system, which I want to include some form of status information as far as the mail sending progress goes. I'm trying to mail off around 2000 custom emails, taking about 10 minutes in total. I would like a status screen / window / div / something which will tell me how far it is, and how many it still has to fire off.

I've tried everything from div's to popups, using sleep() and usleep(), practically everything short of a java applet, which would ultimatelly not be suitable. It seems my status I'm trying to update only displays once the page has finished the script, even if I put echo's inside the while loop which sends the mail messages!

Any help will be greatly appreciated! [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

#2 Honoré

  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationAntwerp - Belgium

Posted 01 June 2006 - 02:00 PM

You could do this with a PHP GTK desktop application.

#3 wisewood

  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 01 June 2006 - 03:01 PM

PHP is a server-side script, so all the code is run on the server and the result is output to the browser afterwards.

If you wanted to do something like "pause" the script to take a snapshot of its progress, you'd have to get very creative. Using ajax you might be able to do something that might give the desired result, but i wouldnt want to guess at how would be best to do it.

I have a script which uses AJAX to check for new entries to a mySQL table every 60 seconds. A javascript timer counts down from 60 seconds, and when it hits zero, the query runs, and updates a div on the page with the resulting number of new entries in the table. Because its done with AJAX, the page doesnt reload, only the div is refreshed.

You could, probably, with some hard work find a way to make this work with your mail script - but you'd have to pass the data to the send-mail script in batches, or one at a time, then have it update the div after each div.

I'd love to give it a go and see what i can come up with, but just havent got the time at the moment.
wisewood: proven fact, I am both wise, and wooden.

#4 countnikon

  • Members
  • PipPip
  • Member
  • 19 posts

Posted 01 June 2006 - 04:49 PM

You have to use ajax for that. Go to [a href=\"http://www.ajaxfreaks.com\" target=\"_blank\"]http://www.ajaxfreaks.com[/a] for tutorials on how to use it. It's really pretty simple once you get your JavaScript down. If you need any help, email me at sheeran.michael at gmail.com. I've been doing a ton of AJAX Apps for the company I work for and would be more than happy to help.

#5 .josh

  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 01 June 2006 - 06:01 PM

yeah. basically you could do something like this:

-query db for total emails.

-make a "batch" variable that represents 1% of the emails
ex: $batch= $total_emails * .01;

then have ajax run the query by the batch, updating the % each round.

pseudo code:

$batch = $_REQUEST['batch'];
$min = (($_REQUEST['round'] * $batch) - $batch) + 1;
$max = ($_REQUEST['round'] * $batch);
if ($batch and $round) {  
   $sql = "select email from table asc limit $min, $max";
   $rs = mysql_query($sql);
   while ($list = mysql_fetch_array($rs)) {   
       $email = $list['email'];
       //send mail to $email
$output = 'done';

if (!$_POST['submit'] {
   $sql = "select count(email) as total_emails from table";
   $rs = mysql_query($sql);
   $total_emails = mysql_fetch_array($rs['total_emails']);
   $batch = ceil($total_email * .01);

<script lang = 'javascript'>
   var batch = <?=$batch;>;

/*  insert the ajax functions here that create the object, and 
send 2 variables to process_emails.php : batch and round ...
see the ajaxfreaks.com tutorials for setting up the object and 
passing vars to the target php script */

/* make a simple js loop 1 to 100 to call the ajax function
that sends the info to the php script */
function blah() { 
var x;
   for (x=0;x<100; ++x) {
      //call ajax function, named processmail for example
percent complete:<br>
<div id='percent'></div>
<form name='blah' ... >
  <input type = 'submit' name = 'submit' value='start emailing' onclick ='blah()'>

okay that's pretty much everything you need to know... if you go to the form validation tutorial on ajaxfreaks.com you can see how to do the css that will update the <div id='percent'>0</div> content.

maybe later i will officially write out all the code but i'm about out of time before i have to go to work. maybe i'll do it tonight, if you can't figure it out from here. basically what you want to do is follow the same concepts as an ajax form validation for your main page, and for your processing php file, follow the concepts of pagination.

this pseudo script basically finds out what 1% of the total emails is, like say you have 2000 email addresses to email then it will send out emails in batches of 20, updating to 2%, 3%, etc...

of course, you will have to tweak it if you want it to display differently. like say, if you wanted to show the current email address being emailed, and show the percentage based on individual email (incremented by .005 in the case of 2000 emails) then you would have to alter some stuff. for instance, you wouldn't need to do the limit in the in a pagination fashion. you could simply keep track of the current id associated with the email and select based on the next one, and the php script would only process one email at a time. it all depends on how you wish to display it, as far as how your code is structured.

good luck!
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 lonewolf277

  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 June 2006 - 07:20 AM

Wow, thank you for the great response! I didn't expect this much attention to my post so soon! [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]

I'm off to check out AJAX stuff, will post any useful information I find [other than the stuff already posted here]...

#7 wisewood

  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 02 June 2006 - 07:45 AM

I mocked up a ver rough test for the principle last night, and it does work using the ajax idea. I only did it sending one email at a time, rather than a batch, which obviously takes longer, but you'll definately find a way around it this way.
wisewood: proven fact, I am both wise, and wooden.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users