timecatcher Posted July 25, 2009 Share Posted July 25, 2009 Ok im in a competition for the best register.php script, both secure customizable and bug free. So you may see a few more of these posts, sorry guys. You have a GOOD reputation of providing the answers I need 99% of the time when im having a blond moment. So heres my question, Is there any easier way of listing all the years since 1900-2009 without typing and c&p. (Oh, and if so what is it, im sure its plainly simple as it seems like it is in my head but I just can't think of a way.) Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/167386-birthdate/ Share on other sites More sharing options...
ignace Posted July 25, 2009 Share Posted July 25, 2009 $years = range(1900, 2009); P.S. w00t! 1000 posts almost a grown-up now Quote Link to comment https://forums.phpfreaks.com/topic/167386-birthdate/#findComment-882604 Share on other sites More sharing options...
timecatcher Posted July 25, 2009 Author Share Posted July 25, 2009 Thankyou I will give that a shot, I don't know exactly how to implement it but trial and error didn't do anyone halm. Timecatcher. Quote Link to comment https://forums.phpfreaks.com/topic/167386-birthdate/#findComment-882636 Share on other sites More sharing options...
.josh Posted July 25, 2009 Share Posted July 25, 2009 no point in using range, as you will have to loop through to display it anyways. echo "<select name='year'>"; for ($x = 1900;$x<=2009; $x++) { echo "<option value='$x'>$x</option>"; } echo "</select>"; Quote Link to comment https://forums.phpfreaks.com/topic/167386-birthdate/#findComment-882652 Share on other sites More sharing options...
jonsjava Posted July 25, 2009 Share Posted July 25, 2009 for a more dynamic approach (just playing on Crayon Violent's script): <?php $this_year = date("Y"); $last_year = $this_year - 1; echo "<select name='year'>\n"; echo "\t<option value='$this_year' selected='selected'>$this_year</option>\n"; for ($x = $last_year;$x>=1900; $x--) { echo "\t<option value='$x'>$x</option>\n"; } echo "</select>\n"; *edit remembered that most people have the most current year first. fixed the code to reflect that. Quote Link to comment https://forums.phpfreaks.com/topic/167386-birthdate/#findComment-882683 Share on other sites More sharing options...
timecatcher Posted July 25, 2009 Author Share Posted July 25, 2009 Foreach ALWAYS throughs me, but lets see. I tried to learn that part again yesterday we shall see. Thanks guys. Timecatcher. Quote Link to comment https://forums.phpfreaks.com/topic/167386-birthdate/#findComment-882705 Share on other sites More sharing options...
.josh Posted July 25, 2009 Share Posted July 25, 2009 well there isn't a foreach in any of the code examples posted. But anyways, a foreach is actually pretty simple: You have an array and for each value (element) in the array, you execute the code. $array = array('a','b','c'); foreach($array as $value) { // execute code here } So in that loop, $value represents the current element value. So in the first iteration, $value == 'a', 2nd iteration, $value == 'b', etc... if you need to know the element's key, you can do this: $array = array('a','b','c'); foreach($array as $key => $value) { // execute code here } so on first iteration, $key == 0, 2nd iteration, $key == 1, etc... You can read this tutorial for more info about foreach loops (and other loops in general): http://www.phpfreaks.com/tutorial/php-loops Quote Link to comment https://forums.phpfreaks.com/topic/167386-birthdate/#findComment-882719 Share on other sites More sharing options...
timecatcher Posted July 25, 2009 Author Share Posted July 25, 2009 Oh hehe I meant, any type of 'for statement' sorry. But thanks anyway this will be useful. Timecatcher . Quote Link to comment https://forums.phpfreaks.com/topic/167386-birthdate/#findComment-882732 Share on other sites More sharing options...
ignace Posted July 25, 2009 Share Posted July 25, 2009 no point in using range, as you will have to loop through to display it anyways. echo "<select name='year'>"; for ($x = 1900;$x<=2009; $x++) { echo "<option value='$x'>$x</option>"; } echo "</select>"; party p00per Quote Link to comment https://forums.phpfreaks.com/topic/167386-birthdate/#findComment-882859 Share on other sites More sharing options...
.josh Posted July 25, 2009 Share Posted July 25, 2009 If it helps, it was with great sadness and a heavy heart that I posted that, as range is one of my better liked functions. Quote Link to comment https://forums.phpfreaks.com/topic/167386-birthdate/#findComment-882861 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.