Jump to content

Birthdate


timecatcher

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.