Trium918 Posted March 2, 2007 Share Posted March 2, 2007 I need to know how to rest a for loop back to 1. for( $i = 1; $i <= 10; $i++) { print "$i"; } <input type=submit value=rest> Link to comment https://forums.phpfreaks.com/topic/40819-how-to-rest-a-for-loop/ Share on other sites More sharing options...
fert Posted March 2, 2007 Share Posted March 2, 2007 $i=1; Link to comment https://forums.phpfreaks.com/topic/40819-how-to-rest-a-for-loop/#findComment-197633 Share on other sites More sharing options...
benjaminbeazy Posted March 2, 2007 Share Posted March 2, 2007 that would make an infinite loop Link to comment https://forums.phpfreaks.com/topic/40819-how-to-rest-a-for-loop/#findComment-197634 Share on other sites More sharing options...
Trium918 Posted March 2, 2007 Author Share Posted March 2, 2007 This for loop runs 7 times. When the loop reaches it ending, I need away to rest the loop back to 1 when the user clicks the correct button. <? //Simple Array tha has a value //assigned to it $remark[1] = "My first number is:"; $remark[2] = "My Second Guess is:"; $remark[3] = "Three"; $remark[4] = "Four"; $remark[5] = "Five"; $remark[6] = "Six"; $remark[7] = "Seven"; echo "<form method=\"GET\">"; echo "<input type=\"submit\" name=\"Guess\" value=\"Guess\">"; echo "<input type=\"submit\" name=\"Higher\" value=\"Higher\">"; echo "<input type=\"submit\" name=\"Lower\" value=\"Lower\">"; echo "<input type=\"submit\" name=\"Correct\" value=\"Correct\">"; $valuecheck = $_GET['count']; //For Loop for ($i = 1; $i <= 7; $i++) { if( $valuecheck == $i){ process(); } }//End of the For Loop $count++; //Counter function process(){ global $guess,$max,$min,$count,$remark,$i; if($_GET["Guess"]){ $guess=50; print"<h2>$remark[$i] $guess<br></h2>"; $max = 100; $min = 0; } else if($_GET["Higher"]){ $min = $guess; $guess = round($guess + (($max - $min)/2)); print"<h2>$remark[$i] $guess <br></h2>"; print"max = $max min = $min </h2>"; print"count = $count"; } else if($_GET["Lower"]){ $max = $guess; $guess = round($guess -(($max - $min)/2)); print"<h2>$remark[$i] $guess <br></h2>"; print"max = $max min = $min </h2>"; print"count = $count"; } }//End of function process() if($_GET["Correct"]){ } echo "<input type=\"hidden\" name=\"count\" value=\"$count\">"; echo "<input type=\"hidden\" name=\"guess\" value=\"$guess\">"; echo "<input type=\"hidden\" name=\"max\" value=\"$max\">"; echo "<input type=\"hidden\" name=\"min\" value=\"$min\">"; echo"</form>"; ?> Link to comment https://forums.phpfreaks.com/topic/40819-how-to-rest-a-for-loop/#findComment-198257 Share on other sites More sharing options...
The14thGOD Posted March 2, 2007 Share Posted March 2, 2007 reset($i); at the end of the last elseif in your function? Link to comment https://forums.phpfreaks.com/topic/40819-how-to-rest-a-for-loop/#findComment-198261 Share on other sites More sharing options...
Trium918 Posted March 3, 2007 Author Share Posted March 3, 2007 Ok, I used the rest($i) inside an if statement and it rest $i but I end up having to click Guess twice. How can I reconstruct the code where everytime the user clicks Guess the game starts over? if($_GET["Correct"]){ rest($i); } Link to comment https://forums.phpfreaks.com/topic/40819-how-to-rest-a-for-loop/#findComment-198267 Share on other sites More sharing options...
kenrbnsn Posted March 3, 2007 Share Posted March 3, 2007 The word is "reset" not "rest". The function is reset(), not rest(). But that won't help you. Just put <?php $count = 1; ?> in this area: <?php if($_GET["Guess"]){ $guess=50; print"<h2>$remark[$i] $guess<br></h2>"; $max = 100; $min = 0; $count = 1; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/40819-how-to-rest-a-for-loop/#findComment-198270 Share on other sites More sharing options...
Trium918 Posted March 3, 2007 Author Share Posted March 3, 2007 The code is still counting even when I used I apply this count = 1; to if($_GET["Guess"]) <?php $count = 1; ?> in this area: <?php if($_GET["Guess"]){ $guess=50; print"<h2>$remark[$i] $guess<br></h2>"; $max = 100; $min = 0; $count = 1; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/40819-how-to-rest-a-for-loop/#findComment-198275 Share on other sites More sharing options...
Trium918 Posted March 3, 2007 Author Share Posted March 3, 2007 The code is still counting even when I used I apply this count = 1; to if($_GET["Guess"]) <?php $count = 1; ?> in this area: <?php if($_GET["Guess"]){ $guess=50; print"<h2>$remark[$i] $guess<br></h2>"; $max = 100; $min = 0; $count = 1; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/40819-how-to-rest-a-for-loop/#findComment-198837 Share on other sites More sharing options...
sspoke Posted March 4, 2007 Share Posted March 4, 2007 xor $i,$i Link to comment https://forums.phpfreaks.com/topic/40819-how-to-rest-a-for-loop/#findComment-198864 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.