Solved

# loop equation needed!

Posted on 2006-05-09
Medium Priority
250 Views
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);
\$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!
0
Question by:tgavin
• 2

LVL 13

Accepted Solution

kamermans earned 1000 total points
ID: 16641958
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);
\$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

LVL 16

Expert Comment

ID: 16642029
<?
\$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>" ;

//
//

if (\$count<\$totalCount)
echo "<script>
location.href=\"sendMail.php?count=".(\$count+1)."\";
</script>";
else
echo "DONE";
?>
0

LVL 13

Expert Comment

ID: 16642154
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

Author Comment

ID: 16644525
Thank you very much. That's just what I was looking for! :)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
###### Suggested Courses
Course of the Month13 days, 22 hours left to enroll