Hi all,

I've been posting a lot here so I'm sure you're all getting bored with me 😃 I am just about done now though!

One thing I am struggling with is using in_array(). I can use it fine with an array I just made like $xyz = array("x", "y", "z"); but how would I make it work with a database when I have $xyz = mysql_fetch_array($blah); I tried just doing if(in_array("blah", $xyz) { ... } but that didn't work. I wasn't expecting it to because the info I need is in $xyz['day'] (the 'day' column in the MySQL database) and you can't do $xyz['day']['1' etc. so I'm a little confused on how I can do this? Maybe I have to use a loop function? Please advise!

Thanks
Max

    Put the code aside, and think about what you are trying to do.

    Are you trying to check that a certain value is in a given array?

      Yes, I've got a drop-down menu on one of the forms on my site. But it's a kids site and they get quite mischevious when they want to cheat the game to get extra points 😃 so I need to check that they did actually select one of the options on the drop-down menu, and not create their own nice little form and post it 🙂

      Thanks
      Max

        This means that the array that you are talking about is one that you create youself.
        It contains the range of possible values for a given drop down menu.

        Now, my guess is that you have stored these possible values in your database, hence you are trying to retrieve them to test with the incoming value.

        The solution therefore, would be to select the column containing these possible values, and then loop through the result set using mysql_fetch_array().
        On each iteration, the value of the current field would be added as an element in an array.
        After that, you can use in_array() on this array with the user's incoming value to test.

        There may be an easier method still.
        Attempt to retrieve the row in the table where the field's value corresponds to the user's incoming value.
        If no rows are returned, then the value does not exist, and so is invalid.

          Ah yes, that last idea was a good one 😃 Why didn't I come up with that before?

          Thanks !

            Write a Reply...