Jump to content

Recommended Posts

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??

Link to comment
https://forums.phpfreaks.com/topic/69065-run-the-code-more-than-once/
Share on other sites

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

 

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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