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
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.

 

Link to comment
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.

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.