hello everyone,
I am making a job portal site. the job seeker in my site enters his date of birth by using three drop down lists (date,month,year). this value i concatenate and insert into database as (1990-09-28). when the user presses the edit button , i want to retrieve this date and display in the drop down list. i first explode the string value i recieve from database into an array. so i have values like :
$datearray[0]=>1980
$datearray[1]=>09
$datearray[2]=>28
now i want the same values to appear selected in my drop down list. I have made a function make_date_list(),and i use it for displaying the drop down list of date where i want. i dont want to use the if condition for it will be very long code.
please forward any suggestions.

try:

<?php
$datearray=explode('-','1980-09-28');

$days=str_replace('>'.$datearray[2],' selected="selected">'.$datearray[2],preg_replace('#option>(\d+)#','option value="$1">$1','<option>'.implode('</option><option>',range(1,31)).'</option>'));
echo '<select name="day">'.$days.'</select>';
?>

dear hielo,
i dont understand this code
please explain
my drop down list for date is already created and displayed. i made a function and am calling it

print '<select name="day">';
for ($day = 1; $day <= 31; $day++) 
{
print "\n<option value=\"$day\">$day</option>";
}
print '</select>';
$datearray=explode('-','1980-09-28');

this gives me the date also {$datearray[2] = 28.
how am i supposed to get that 28 preselected when the user loads the edit.php page?

please help
thanks

Member Avatar for diafol

You have to be careful with this as displaying, say:

2008 | 03 | 30 in a dropdown is fine, but then if somebody changes 03 to 02, the date becomes invalid.

Also you need to check leap years in order to include 29th Feb.
This should be done whenever the year or month dropdowns are changed

Anyway - can't you use a datepicker?

Here's jqueryui:

dear ardav
i cant use a date picker as i have specific instructions from my instructor.
and no need to worry about leap year or feb 30 31.
thanks

Member Avatar for diafol

In that case, hielo's code was perfect for you. The only change I'd make would be to the first line:

$datearray=array_map('intval',explode('-','1980-09-28'));

$days=str_replace('>'.$datearray[2],' selected="selected">'.$datearray[2],preg_replace('#option>(\d+)#','option value="$1">$1','<option>'.implode('</option><option>',range(1,31)).'</option>'));

$months=str_replace('>'.$datearray[1],' selected="selected">'.$datearray[1],preg_replace('#option>(\d+)#','option value="$1">$1','<option>'.implode('</option><option>',range(1,12)).'</option>'));

...

The array_map with intval ensures 09 appears as 9 etc. Otherwise, you'll need to do a str_pad with 0 on each value in the ranges.

OK, then if you don't understand my post, change your code as follows:

print '<select name="day">';
for ($day = 1; $day <= 31; $day++) 
{
  if($datearray[2]!=$day)
    print "\n<option value=\"$day\">$day</option>";
  else
    print "\n<option value=\"$day\" selected=\"selected\">$day</option>";

}
print '</select>';

thanks hielo
i did it with little alteration

<?php
for ($year = 1; $year <= 30; $year++) 
{
if($row3['years']==$year)
{
echo '<option selected="selected">';
echo $year;
echo'<br />';
echo '</option>';
}
else
{echo '<option>';
echo $year;
echo'<br />';
echo '</option>';
}}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.