Jump to content

Archived

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

asgsoft

Loops

Recommended Posts

Why does this loop do one more than it is supposed to?

[code]
<?php
if(isset($_POST['submit']))
{
$itemcount = $_POST['item_count'];
$petitionid = $_POST['petitionid'];
$i=-1;
do
{
$i++;
$to = $_POST["friendemail_".$i.""];
$subject = "My HTML email test.";
$headers = "From: admin@site.com";
$headers .= "text/html; charset=ISO-8859-1rn";

$message = "<html><body>";
$message .= "<h1> Hello " . $_POST["friendname_".$i.""] ."</h1><br> Your friend would like you to have a look at this petition and sign it if posible located at http://wwww.site.com/show.php?id=".$petitionid."";
$message .= "</body></html>";

if (mail($to,$subject,$message,$headers) ) {
   echo "The email has been sent!";
   } else {
   echo "The email has failed! ";
   echo "$to";
  
   }
  
}
while ($i<$itemcount);
}
?>[/code]

If $itemcount is 2 it loops 3 times.

Share this post


Link to post
Share on other sites
That's exactly what it is supposed to do.
After the first execution of the loop $i==0, so the loop is executed again.
After the second execution of the loop $i==1, so the loop is executed once more.
After the third execution of the loop $i==2, so the loop is not executed anymore.
You have executed the loop 3 times, what is exactly what you have programmed.

Share this post


Link to post
Share on other sites
If $itemcount represents the number of times it is posted then you could follow the advise of shocker-z.
[!--quoteo(post=365253:date=Apr 16 2006, 10:59 AM:name=asgsoft)--][div class=\'quotetop\']QUOTE(asgsoft @ Apr 16 2006, 10:59 AM) [snapback]365253[/snapback][/div][div class=\'quotemain\'][!--quotec--]
How can I make it do 3 times when it is posted 3 times?
[/quote]

Share this post


Link to post
Share on other sites
OK i did, it does two loops like it should but only one is sending the exail rather than both, so there is an empty loop.

Share this post


Link to post
Share on other sites
[!--quoteo(post=365258:date=Apr 16 2006, 11:14 AM:name=asgsoft)--][div class=\'quotetop\']QUOTE(asgsoft @ Apr 16 2006, 11:14 AM) [snapback]365258[/snapback][/div][div class=\'quotemain\'][!--quotec--]
OK i did, it does two loops like it should but only one is sending the exail rather than both, so there is an empty loop.
[/quote]
May be you should put the increment of $i at the end of the do loop such as this:
[code]
<?php
if(isset($_POST['submit']))
{
$itemcount = $_POST['item_count'];
$petitionid = $_POST['petitionid'];
$i=0;
do
{
$to = $_POST["friendemail_".$i.""];
$subject = "My HTML email test.";
$headers = "From: admin@site.com";
$headers .= "text/html; charset=ISO-8859-1rn";

$message = "<html><body>";
$message .= "<h1> Hello " . $_POST["friendname_".$i.""] ."</h1><br> Your friend would like you to have a look at this petition and sign it if posible located at http://wwww.site.com/show.php?id=".$petitionid."";
$message .= "</body></html>";

if (mail($to,$subject,$message,$headers) ) {
   echo "The email has been sent!";
   } else {
   echo "The email has failed! ";
   echo "$to";
  
   }
$i++;
}
while ($i<$itemcount);
}
?>
[/code]

Share this post


Link to post
Share on other sites
OK don't worry, I solved it.


I had to make the increasemnt the last command in the loop so now its perfect. Thanks for all your help anyway

edit//

Honoré you beat me to it.

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.