Jump to content

Dynamically Check in_array?


Monkuar

Recommended Posts

Okay, this is my loop to show the birthday for Years.

<?php
    for ($i=2006; $i>=1900; $i=$i-1)
    {
     echo "<option value='$i'>$i</option>";
    }
   ?>

 

It will show 2012 to 1900 as a dropdown menu.. in my select form.

 

 

Now I want to check the user input against this loop.

 

The form is:

 

<select name="BirthDay[]">
   <?php
    for ($i=2012; $i>=1900; $i=$i-1)
    {
     echo "<option value='$i'>$i</option>";
    }
   ?>
   </select>

 

Now, I want to use in_array function to check if it equals any of the values I looped, if it doesn't echo out "Hacker!"

 

 

Here is my in_array

 

 

 

 

$bday = array( NEED TO PUT THIS LOOP HERE SOMEHOW?!?!
);
if (!in_array($_POST['BirthDay'], $bday)) {
message("Please select a Correct Birthday, or u trying to hack");
}

 

 

Thanks if u can help!

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/258233-dynamically-check-in_array/
Share on other sites

for ($i=2012; $i>=1900; $i=$i--)
{
echo "<option value='$i'>$i</option>";
$bday[] = $i; 
}

if (!in_array($_POST['BirthDay'], $bday)) {
message("Please select a Correct Birthday, or u trying to hack");
}

 

Couple of things...

1. Why not use "$i=$i--" instead of "$i=$i-1"?

2. Why would you want to dynamically make an array of something that you know for a fact is not going to change? Save yourself some resources and do a static array of the years instead of running it every single time.

3. I really hope that is not the kind of error messages you give your users.

 

for ($i=2012; $i>=1900; $i=$i--)
{
echo "<option value='$i'>$i</option>";
$bday[] = $i; 
}

if (!in_array($_POST['BirthDay'], $bday)) {
message("Please select a Correct Birthday, or u trying to hack");
}

 

Couple of things...

1. Why not use "$i=$i--" instead of "$i=$i-1"?

2. Why would you want to dynamically make an array of something that you know for a fact is not going to change? Save yourself some resources and do a static array of the years instead of running it every single time.

3. I really hope that is not the kind of error messages you give your users.

 

If I don't use $i=$i-1, and use the $i-- there is a 2nd delay lag when running the script :P

Dunno why, but there is, I switched it back to $i=$i-1 and it's working fine.

 

Thanks for your help  :o

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.