Monkuar Posted March 4, 2012 Share Posted March 4, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/258233-dynamically-check-in_array/ Share on other sites More sharing options...
marcbraulio Posted March 4, 2012 Share Posted March 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258233-dynamically-check-in_array/#findComment-1323703 Share on other sites More sharing options...
Monkuar Posted March 4, 2012 Author Share Posted March 4, 2012 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 Dunno why, but there is, I switched it back to $i=$i-1 and it's working fine. Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/258233-dynamically-check-in_array/#findComment-1323708 Share on other sites More sharing options...
marcbraulio Posted March 4, 2012 Share Posted March 4, 2012 No problem, best of luck! Quote Link to comment https://forums.phpfreaks.com/topic/258233-dynamically-check-in_array/#findComment-1323709 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.