Jump to content

Archived

This topic is now archived and is closed to further replies.

lonewolf277

Pausing PHP Script to update display

Recommended Posts

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\" /]

Share this post


Link to post
Share on other sites
You could do this with a PHP GTK desktop application.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:

process_emails.php
[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';
[/code]

index.php
[code]
<?php
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
      processmail(x);
   }
}
</script>
percent complete:<br>
<div id='percent'></div>
<form name='blah' ... >
  <input type = 'submit' name = 'submit' value='start emailing' onclick ='blah()'>
...
</form>
[/code]

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!

Share this post


Link to post
Share on other sites
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]...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.