PHP: Loop?

Hitman
Soldato
Joined
25 Feb 2004
Posts
2,837
Hi there,

I seem to be making random forms lately and am creating another with a crazy idea :p

Say If I have a form with a text box, if I enter "1" into this text box then hit "Submit", it'll do whatever PHP it's supposed to. What I'm after is that if I enter "2" or "3", it'll do it 2 times, or 3 times (so it'll do it how ever many times I specified on the form).

Is this possible? If so, how? :p

Cheers
 
Code:
$number = filter_input(INPUT_GET, 'number', VALIDATE_INT);
if ($number) {
    for ($i = 0; $i < $number; $i ++) {
        /* loop will run however many times you entered */
    }
}
 
Last edited:
PHP:
$loops = intval($_POST['input']);

for ($i = 0; $i < $loops; $i++) {
    echo "loop " . $i;
}

Didn't actually test it :rolleyes: Let me know if it works
 
Every element in your form has a name=" " and value = " " attribute. When you hit submit, all go into the $_POST[] array. You can access all the elements therefore using $_POST['somevariable'].

As the above example, if you had a textbox with name="input" in your form then when you do $myvalue = $_POST['input']; in your PHP script you will get the value inside the textbox.
 
Back
Top Bottom