MrXander Posted September 12, 2007 Share Posted September 12, 2007 Hi there, I am trying to get my code running more than once when a user submits their form. For example, when a user vists the page called "search", then can enter a number between 1 and 100. Once they hit submit, I want the code to run completely, x amount of times, depending on what the user requests. Can anyone help?? Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2007 Share Posted September 12, 2007 make it a function and use a for loop. Quote Link to comment Share on other sites More sharing options...
MrXander Posted September 12, 2007 Author Share Posted September 12, 2007 Example? ??? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted September 12, 2007 Share Posted September 12, 2007 for/while example <?php $num = $_GET['count']; $i = 1; while($i<=$num){ //Do some stuff $i++; } ?> you can replace while with for and have all your $i declaration in the naming of the loop i.e <?php $num = $_GET['cout']; for($i=1;$i<=$num;$i++){ //do some stuff } ?> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 12, 2007 Share Posted September 12, 2007 Create a form, make it submit to itself (the current page) and use the get as the method. Create a text field called loop and a submit button. Then in your PHP code check to make sure $_GET['loop] exists and that is of a numerical value, using isset and is_numeric within an if statement. If $_GET['loop] variable exists and is of a numerical value use it in your for loop. it really is straight forward. From reading the above you should be able to accomplish what you are trying to do. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted September 12, 2007 Share Posted September 12, 2007 Just thought I note that a for construct is a much more rigid system than the while construct. This is because a for construct must always return true and do the next step (as long as condition B is true $i<=$num) however in a while construct you can use logic to test if $i should advance or not such as if($i%2 = 0 ) {$i++;} else{$i=$i;} both have advantages, but in general I'd use a while loop for most things because it just makes more sense. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.