Jump to content

Loops


asgsoft

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.