-1

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.

7
  • You made my day. Why isn't it is_numeric($_GET['rows']) && $_GET['rows']>0 && $_GET['rows']<11 ? Commented Oct 14, 2013 at 9:51
  • 3
    These errors notwithstanding, validating integer values with is_numeric is suboptimal because is_numeric also allows floating point and scientific notation. If you want "digits only" then use ctype_digit (or a regular expression). Commented Oct 14, 2013 at 9:53
  • @Jon yes, fair enough, but OP didn't mentioned that rows should be int Commented Oct 14, 2013 at 9:54
  • 1
    is_numeric($i)? You define $i just one line before as $i = 1! Commented Oct 14, 2013 at 9:56
  • Earlier I was trying to assign $rows to $_GET['rows'] and put if (is_numeric($rows)), but this didn't help any, I've kept it in case there's some way I can use it still. Commented Oct 14, 2013 at 9:56

4 Answers 4

3

intval returns 0 if a non integer is passed to it. Then check it is between range.

 $rows = $_GET['rows'];

 if (($value = intval($rows)) !== 0 && 1 <= $value && $value <= 10) {
    echo "Number is ok";
 } else {
    echo "Please enter a valid number between 1 and 10.";
 }

Without setting variable in if:

 $rows = $_GET['rows'];
 $value = intval($rows);

 if (1 <= $value && $value <= 10) {
    echo "Number is ok";
 } else {
    echo "Please enter a valid number between 1 and 10.";
 }

intval: http://www.php.net/manual/en/function.intval.php

Sign up to request clarification or add additional context in comments.

12 Comments

Declaring variables in ifs is very bad idea.
@ElonThan why is it a bad idea?!
@ElonThan well; here he's using a temporary variable, just for use in the if. That's a perfectly valid use case.
@ElonThan: Then how do you explain while($row = mysqli...) stuff?
@bwoebi: It's a matter of preference. Compared to assigning independently, this way you are saving (typing less) a $value -- 6 chars. That's not a whole lot of argument, so if the "verbose" version reads better to you then that's the one to use.
|
0

May be you need this..And why are you iterating it.?

 $rows = $_GET['rows'];
 if (is_numeric($rows) && $rows <= 10 && $rows >= 1) {
    echo "Number is ok";
 } 
 else {
    echo "Please enter a valid number between 1 and 10.";
 }

2 Comments

Not well since negative values will pass (floats as well, but OP didn't mentioned about that)
@User016: Oh, you edited the answer in the meanwhile. I've removed my previous comment.
0

You can try this.

 $rows = (isset($_GET['rows']) ? $_GET['rows'] : 0);

 if (is_numeric($rows) && $rows >= 1 && $rows <= 10) {
    echo "Ok";
 } 
 else {
    echo "Fail";
 }

Comments

0

Simply cast to int and use regular conditions:

$rows = (!empty($_GET['rows']) ? (int)$_GET['rows'] : 0);
//Cast it!                       ^^^^^

if ($rows >= 1 && $rows <= 10) {
    //We got a match!
} else {
    //We don't have a match!
}

Note: This will also turn 1.234 into 1

5 Comments

This will turn almost everything into 0.
Accepting abc as valid input clearly violates the alert the user if they input a non-numeric input part of the spec.
@ÁlvaroG.Vicario Which could easily be fixed by placing anything inside the else clause.
@h2ooooooo: What if I enter 4chan? No matter how you cut it, the int cast is a bad way to validate.
And you're even discarding actual zeroes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.