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: [email protected]";
$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
https://forums.phpfreaks.com/topic/7531-loops/
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
https://forums.phpfreaks.com/topic/7531-loops/#findComment-27417
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
https://forums.phpfreaks.com/topic/7531-loops/#findComment-27424
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: [email protected]";
$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
https://forums.phpfreaks.com/topic/7531-loops/#findComment-27426
Share on other sites

Archived

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

×
×
  • 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.