PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
i have created an array to take 10 values and output them in ascending and descending order. i now need to modify it so that it asks for the number of elements to be input, starting with a default of 10. this is the bit im struggling to work out.
Use a for loop and loop to the supplied number of iterations. Of course, you should validate the input the user offers to make sure it is numeric, greater than 0, things like that. But the loop is fairly straight forward.
<?php
$limit = 10; // Set a default
$max = 100; // Set a maximum so the user doesn't loop your server into oblivion
$min = 1; // Set a minimum so you can actually have something to show
// DId the user send you something?
if (isset($_POST['userinput'])) {
$cap = $_POST['userinput'];
// Is the something the user sent acceptable?
if (is_numeric($cap) && $cap >= $min && $cap <= $max) {
$limit = $cap;
}
}
// Start the loop, at one since your inputs and outputs are 1 based
for ($i = 1; $i <= $limit; $i++) {
// echo your stuff here using $i as your counter
}
?>
Play around with this a little bit. I am sure you can use it as a basis for something like what you want to do.