Page 1 of 1

I need a number!

Posted: Wed Mar 09, 2011 2:54 pm
by JustPlainJef
I'm trying to allow a positive integer, or zero, for scores. I tried this (for both AwayScore and HomeScore):

Code: Select all

(filter_var($gamescore['AwayScore'], FILTER_VALIDATE_INT, array('Options'=>array('min_range'=>0))))
And it didn't work. So I tried pulling the scores out of the $Gamescore array and plugging them into their own variables, and that didn't work either.

I can put in positive or negative numbers, but not zero. I want it to basically be allowed to be >= 0.


Any recommendations?

Re: I need a number!

Posted: Wed Mar 09, 2011 4:01 pm
by Weirdan
Please do

Code: Select all

var_dump($gamescore['AwayScore']);
and post the results so that we would actually know what your input data looks like.

Also, if you're using that in an if statement, make sure that you use strict comparison:

Code: Select all

if (false !== filter_var($score, FILTER_VALIDATE_INT, array('options' => array('min_range' => 0)))) {
    // ok
} else {
    // validation failed
}

Re: I need a number!

Posted: Thu Mar 10, 2011 4:19 am
by JustPlainJef
I entered as scores -1 to 1, 0 to 0, and a to b for AwayScore and HomeScore, respectively. Here's the output.

string(2) "-1"
string(1) "1"
Game #8 has been updated.
string(1) "0"
string(1) "0"
Please ensure scores are whole numbers only.
string(1) "a"
string(1) "b"
Please ensure scores are whole numbers only.

So -1 to 1 was the only one that worked. Here's my full code for that section. This is in a secure area so I'm less concerned about user input, but I'll probably escape them when they go live anyway.

First, (#B) the code checks to make sure something was input for both scores, so if they are left blank, we don't do anything. Scores will be entered by the VPs and the scores aren't always reported in a timely manner.

Then, (#C) we are supposed to check to see if they are positive integer, so we can't have scores of -27 to 3, or 3.2 to 2.7, or pi to i.

In #D, we update the records in the MySQL table. If it fails (#E), it says to contact me. If it works (#F), it says "Game #NNN updated."

The bottom ELSE (#G) is the ELSE if #C fails.

Code: Select all

 foreach($_POST as $array)
     {#A
     foreach($array as $gamescore)
         {#B
        #Check to see that something was input
        IF($gamescore['AwayScore']!='' && $gamescore['HomeScore']!='')
           {#C
          #Check to see that scores were positive integers
          IF((filter_var($gamescore['AwayScore'], FILTER_VALIDATE_INT, array('Options'=>array('min_range'=>0)))) AND 
               (filter_var($gamescore['HomeScore'], FILTER_VALIDATE_INT, array('Options'=>array('min_range'=>0)))))
            {#D
            $scoreupdate = "UPDATE Schedule SET AwayScore = '$gamescore[AwayScore]',
             HomeScore = '$gamescore[HomeScore]' WHERE GameID = '$gamescore[GameID]'";
            #Update the records
            IF(!$update = mysqli_query($cxn, $scoreupdate))
               {#E
             echo "Couldn't update record.<br />"
             ."Please contact the WebMaster.";
             }#E
            #Echo update success 
            ELSE
              {#F
              echo "Game #".$gamescore[GameID]. " has been updated.<br />"; 
              }#F
            }#D
           ELSE
            {#G
            echo "Please ensure scores are whole numbers only.<br />";
            }#G
          }#C
        }#B
     }#A
As far as your "strict comparison" comment, if I'm reading you right, I have three IF statements, and the first one [IF($gamescore['AwayScore']!='' && $gamescore['HomeScore']!='')] is OK but the second one [IF(!$update = mysqli_query($cxn, $scoreupdate))] could be improved, but since it has the = in it, I'm not sure how to re-write it......

Re: I need a number!

Posted: Fri Mar 11, 2011 8:56 am
by JustPlainJef
Is this an acceptable answer?

Code: Select all

IF(preg_match("/^[0-9]{1,2}$/", $gamescore['AwayScore'])) AND (preg_match("/^[0-9]{1,2}$/", $gamescore['HomeScore']))