loop equation needed!

I can't get my head around how to write this.

I'm sending email batches, let's say 100 at a time out of 500 total. I already have the process in place to do this, but I don't know how to transcribe what I want to print to the screen.

Basically, on the first loop I want to say "Batch number 1 completed. 100 emails sent. 400 remaining"
On the second loop, "Batch number 2 completed. 200 emails sent. 300 remaining"

And so on.

$count = 0;
$precount = 500;
$queue_size = 100;
$queue_refresh = 25;

$query = mysql_query("SELECT queue_email FROM send_queue LIMIT $queue_size") or die(mysql_error());

while($row = mysql_fetch_assoc($query)) {
      // determine number of emails at the start of each batch
      $count = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM send_queue"),0);
      // build each address
      $to = $row['queue_email'];
      // send the email
      email_html();
      // check for success or failure
      if($sent) { // success
            // mail sent, remove addresses from queue
            $delete_query = mysql_query("DELETE FROM send_queue WHERE queue_id = '$row[queue_id]' AND queue_email = '$row[queue_email]'") or die(mysql_error());
            // refresh the page and start next batch
            echo "<meta http-equiv=\"refresh\" content=\"$queue_refresh;url=$_SERVER['PHP_SELF'];\">";
      } else {
      // sending failed
      echo $error_msg;
      exit;
      }
}

Thanks!
tgavinAsked:
Who is Participating?
 
kamermansConnect With a Mentor Commented:
Try this:

<?php
if(!isset($_GET['count'])){
      $count = 1;
}else{
      $count = $_GET['count'] + 1;
}
if(!isset($_GET['total'])){
      $total = 0;
}else{
      $total = $_GET['total'];
}
$precount = 500;
$queue_size = 100;
$queue_refresh = 25;

$query = mysql_query("SELECT queue_email FROM send_queue LIMIT $queue_size") or die(mysql_error());
while($row = mysql_fetch_assoc($query)) {
     // determine number of emails at the start of each batch
     $count = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM send_queue"),0);
     // build each address
     $to = $row['queue_email'];
     // send the email
     email_html();
     // check for success or failure
     if($sent) { // success
          // mail sent, remove addresses from queue
          $delete_query = mysql_query("DELETE FROM send_queue WHERE queue_id = '$row[queue_id]' AND queue_email = '$row[queue_email]'") or die(mysql_error());
              $total++;
     } else {
     // sending failed
     echo $error_msg;
     exit;
     }
}
echo "Batch number $count completed. $total emails sent. ". $precount - $total ." remaining.<br />";
// refresh the page and start next batch
javascriptRedir($_SERVER['PHP_SELF'] . "?count=$count&total=$total", 2); // the 2 is seconds before redir

function javascriptRedir($page, $seconds){
      $time = $seconds * 1000;
      echo "<br><br>You will be redirected to <a href='$page'>$page</a> in $seconds seconds...";
      echo '<script language="javascript">'."\n".'setTimeout('."'".'window.location.href = "'.$page.'"'."'".', '.$time.');'.'</script>';
}
?>
0
 
dr_dedoCommented:
<?
$count = (!isset($_GET['count']))? 1 : $_GET['count'];
$precount = 500;
$queue_size = 100;
$queue_refresh = 25;
$totalCount = $precount/$queue_size;

echo "Run number $count <br>". ($totalCount-$count). " ramining<br>" ;

//
//
// do your code

if ($count<$totalCount)
echo "<script>
location.href=\"sendMail.php?count=".($count+1)."\";
</script>";
else
echo "DONE";
?>
0
 
kamermansCommented:
tgavin: your delete query should look like this:

$delete_query = mysql_query("DELETE FROM send_queue WHERE queue_id = '".$row['queue_id']."' AND queue_email = '".$row['queue_email']."'") or die(mysql_error());
0
 
tgavinAuthor Commented:
Thank you very much. That's just what I was looking for! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.