I was supposed to build a script to alert the user if they input a non-numeric input or a value greater than 10.
I have this:
<form action="" method="get">
<p>Iterations:
<input type="text" name="rows" size="5">
<input type="submit" value="Loop">
</p>
</form>
<table border='1' cellpadding='3' cellspacing='0' bordercolor='#669999' align="center">
<?php
$rows = $_GET['rows'];
for ( $i = 1; $i <= 10; $i++) {
if (is_numeric($i)) {
echo "<tr><td>Iteration is $i</td></tr>";
}
else {
echo "Please enter a valid number between 1 and 10.";
}
}
?>
But it will only return a list of Iteration is 1-10, I can't get it to iterate the number of times I input?
Edit: The loop is actually essential to this.
I'm building on a previous script that would loop like so if, say 5, were entered:
Iteration is 1
Iteration is 2
Iteration is 3
Iteration is 4
Iteration is 5
So this is why I am iterating it and looping it.
Thank you in advance for any advice.
EDIT #2
I've fixed it everyone.
<?php
$rows = $_GET['rows'];
$value = intval($rows);
if (1 <= $value && $value <= 10) {
for ( $i = 1; $i <= $rows; $i++ ) {
echo "<tr><td>Iteration is $i</td></tr>";
}
} else {
echo "Please enter a valid number between 1 and 10.";
}
?>
I've used a combination of $intval to assess the integer of $rows and compare it to see if it's between 1 and 10.
Then I looped it through according to the assignment.
We were supposed to use is_numeric but... this is much cleaner in my mind.
Thanks all!
I'll be answering this after work tomorrow.
is_numeric($_GET['rows']) && $_GET['rows']>0 && $_GET['rows']<11?is_numericis suboptimal becauseis_numericalso allows floating point and scientific notation. If you want "digits only" then usectype_digit(or a regular expression).rowsshould be intis_numeric($i)? You define$ijust one line before as$i = 1!