Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/69785-solved-drop-down-list-from-an-array/
Share on other sites

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

No problem ;D

 

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 ;D

 

~ Chocopi

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.