asgsoft Posted April 16, 2006 Share Posted April 16, 2006 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 More sharing options...
shocker-z Posted April 16, 2006 Share Posted April 16, 2006 $i=-1;Your starting on -1 you need to use $i=0;:)RegardsLiam Link to comment https://forums.phpfreaks.com/topic/7531-loops/#findComment-27416 Share on other sites More sharing options...
Honoré Posted April 16, 2006 Share Posted April 16, 2006 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 More sharing options...
asgsoft Posted April 16, 2006 Author Share Posted April 16, 2006 How can I make it do 3 times when it is posted 3 times? Link to comment https://forums.phpfreaks.com/topic/7531-loops/#findComment-27421 Share on other sites More sharing options...
Honoré Posted April 16, 2006 Share Posted April 16, 2006 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 More sharing options...
asgsoft Posted April 16, 2006 Author Share Posted April 16, 2006 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. Link to comment https://forums.phpfreaks.com/topic/7531-loops/#findComment-27425 Share on other sites More sharing options...
Honoré Posted April 16, 2006 Share Posted April 16, 2006 [!--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 More sharing options...
asgsoft Posted April 16, 2006 Author Share Posted April 16, 2006 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 anywayedit//Honoré you beat me to it. Link to comment https://forums.phpfreaks.com/topic/7531-loops/#findComment-27427 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.