jim.davidson Posted September 18, 2007 Share Posted September 18, 2007 I'm trying to teach myself php. Today's mission learn to build and display arrays I want the array to contain a valid year of birth for anyone between the ages of 16 and 80. I'm pretty sure this code is correct.(though it might be a bit sloppy to all you pros) $dob_dates = array(); $today = date('Y'); $startyear = $today - 80; $endyear = $today - 16; $n = 0; $i = $startyear; while ($i <= $endyear) { $dob_dates[$n] = ($startyear+$n); $i++; $n++; } Now that I've built an array containing valid dob years I want to have a drop down list showing the array. Can someone show me the light on how to do that? Any and all help will be appreciated Quote Link to comment https://forums.phpfreaks.com/topic/69785-solved-drop-down-list-from-an-array/ Share on other sites More sharing options...
chocopi Posted September 18, 2007 Share Posted September 18, 2007 I think you mean something like <select name="select"> <?php for($i = 0; $i <= $n; $i++) { echo "<option name=\"{$dob_dates[$i]}\">{$dob_dates[$i]}</option>"; } ?> </select> That should work ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/69785-solved-drop-down-list-from-an-array/#findComment-350611 Share on other sites More sharing options...
jim.davidson Posted September 18, 2007 Author Share Posted September 18, 2007 Thanks for the help, now I'll sit here and figure out just how it works. That's one of the ways I learn things, take something that works and figure out how and why. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/69785-solved-drop-down-list-from-an-array/#findComment-350628 Share on other sites More sharing options...
chocopi Posted September 19, 2007 Share Posted September 19, 2007 No problem Does it work how you want it to ??? 1. Anyways, it is very simple, its a basic for loop which i) Sets $i to 0, ii) then checks that $i is less than or equal to $n, if not then the loop ends iii) lastly after each loop is will increment $i by 1 until $i is greater than $n 2. It then gets the $i value of the array and then takes it from the variable and puts it within the <option> tag Hope that helps ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/69785-solved-drop-down-list-from-an-array/#findComment-351136 Share on other sites More sharing options...
jim.davidson Posted September 20, 2007 Author Share Posted September 20, 2007 Sorry for the delay in responding...yes it works fine and now I understand it. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/69785-solved-drop-down-list-from-an-array/#findComment-351690 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.