Jump to content


Photo

Loops


  • Please log in to reply
7 replies to this topic

#1 asgsoft

asgsoft
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 16 April 2006 - 08:18 AM

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

<?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);
} 
?>

If $itemcount is 2 it loops 3 times.

Free SEO and Domain tools |HTML ENT| Your Source to expired domains with PageRank & tips


#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 16 April 2006 - 08:35 AM

$i=-1;
Your starting on -1 you need to use $i=0;
:)

Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 Honoré

Honoré
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationAntwerp - Belgium

Posted 16 April 2006 - 08:36 AM

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.

#4 asgsoft

asgsoft
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 16 April 2006 - 08:59 AM

How can I make it do 3 times when it is posted 3 times?

Free SEO and Domain tools |HTML ENT| Your Source to expired domains with PageRank & tips


#5 Honoré

Honoré
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationAntwerp - Belgium

Posted 16 April 2006 - 09:10 AM

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) View Post[/div][div class=\'quotemain\'][!--quotec--]
How can I make it do 3 times when it is posted 3 times?
[/quote]


#6 asgsoft

asgsoft
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 16 April 2006 - 09:14 AM

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.

Free SEO and Domain tools |HTML ENT| Your Source to expired domains with PageRank & tips


#7 Honoré

Honoré
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationAntwerp - Belgium

Posted 16 April 2006 - 09:21 AM

[!--quoteo(post=365258:date=Apr 16 2006, 11:14 AM:name=asgsoft)--][div class=\'quotetop\']QUOTE(asgsoft @ Apr 16 2006, 11:14 AM) View Post[/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:
<?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);
} 
?>


#8 asgsoft

asgsoft
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 16 April 2006 - 09:22 AM

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.

Free SEO and Domain tools |HTML ENT| Your Source to expired domains with PageRank & tips





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users