Hi All,
Here’s the code we settled on (with lots of help from Jason) for anyone who want’s to implement this.
Hope it comes in handy,
Jerry Kornbluth
BACKGROUNDMy client needed to list the dates of birth for her students in the accounts database.
We decided to include date fields for 3 students. Student 1, 2 and 3.
This meant that the parents would have to be able to set up the original database record and also be able to update that record when required.
THE MEMBER APPLICATION FORMIn the original application form, in the PHP code at the top of the page, in the //add user section:
$student_1_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_1'],@$_REQUEST['day_1'],@$_REQUEST['year_1']));
$student_2_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_2'],@$_REQUEST['day_2'],@$_REQUEST['year_2']));
$student_3_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_3'],@$_REQUEST['day_3'],@$_REQUEST['year_3']));
and in the mysql_query("INSERT INTO `{$TABLE_PREFIX}accounts` SET section:
student_1_dob = '".mysql_escape($student_1_dob)."',
student_2_dob = '".mysql_escape($student_2_dob)."',
student_3_dob = '".mysql_escape($student_3_dob)."',
Then in the form, the following code (in the appropriate place for your particular form):
NOTE: You only have to define the variables $lowestYear and $highestYear once per viewer.
<tr>
<td>Date of Birth - Student 1</td>
<td><?php
$lowestYear = 1920;
$highestYear = 2006;
?>
Month:
<select name="month_1">
<?php foreach(range(1,12) as $month_1): ?>
<option value="<?php echo $month_1;?>"><?php echo date("F",strtotime("0000-$month_1"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_1">
<?php foreach(range(1,31)as $day_1): ?>
<option value="<?php echo $day_1;?>"><?php echo $day_1;?></option>
<?php endforeach ?>
</select> Year:
<select name="year_1">
<?php foreach (range($lowestYear,$highestYear) as $year_1):?>
<option value="<?php echo $year_1;?>"><?php echo $year_1;?></option>
<?php endforeach?>
</select>
</tr>
<tr>
<td>Date of Birth - Student 2</td>
<td>Month:
<select name="month_2">
<?php foreach(range(1,12) as $month_2): ?>
<option value="<?php echo $month_2;?>"><?php echo date("F",strtotime("0000-$month_2"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_2">
<?php foreach(range(1,31)as $day_2): ?>
<option value="<?php echo $day_2;?>"><?php echo $day_2;?></option>
<?php endforeach ?>
</select> Year:
<select name="year_2">
<?php foreach (range($lowestYear,$highestYear) as $year_2):?>
<option value="<?php echo $year_2;?>"><?php echo $year_2;?></option>
<?php endforeach?>
</select>
</tr>
And,
<tr>
<td>Date of Birth - Student 3</td>
<td>Month:
<select name="month_3">
<?php foreach(range(1,12) as $month_3): ?>
<option value="<?php echo $month_3;?>"><?php echo date("F",strtotime("0000-$month_3"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_3">
<?php foreach(range(1,31)as $day_3): ?>
<option value="<?php echo $day_3;?>"><?php echo $day_3;?></option>
<?php endforeach ?>
</select> Year:
<select name="year_3">
<?php foreach (range($lowestYear,$highestYear) as $year_3):?>
<option value="<?php echo $year_3;?>"><?php echo $year_3;?></option>
<?php endforeach?>
</select>
</tr>
THE PROFILE UPDATE FORMFor the profile update page, the code at the top of the page, in the //update user section is:
$student_1_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_1'],@$_REQUEST['day_1'],@$_REQUEST['year_1']));
$student_2_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_2'],@$_REQUEST['day_2'],@$_REQUEST['year_2']));
$student_3_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_3'],@$_REQUEST['day_3'],@$_REQUEST['year_3']));
And the code in the $query = "UPDATE `{$TABLE_PREFIX}accounts` SET section is:
student_1_dob = '".mysql_escape($student_1_dob)."',
student_2_dob = '".mysql_escape($student_2_dob)."',
student_3_dob = '".mysql_escape($student_3_dob)."',
In the form, in order to pre-populate the individual parts of the date field, Jason came up with the following code:
<tr>
<td>Date of Birth - Student 1</td>
<td><?php
$lowestYear = 1935;
$highestYear = 2008;
//pre-populate date fields from the database
//get most current $CURRENT_USER
$CURRENT_USER = _websiteLogin_getCurrentUser();
$_REQUEST['month_1']=date("n",strtotime($CURRENT_USER['student_1_dob']));
$_REQUEST['day_1']=date("j",strtotime($CURRENT_USER['student_1_dob']));
$_REQUEST['year_1']=date("Y",strtotime($CURRENT_USER['student_1_dob']));
?>
Month:
<select name="month_1">
<?php foreach(range(1,12) as $month_1): ?>
<option value="<?php echo $month_1;?>" <?php selectedIf($month_1,@$_REQUEST['month_1']);?>><?php echo date("F",strtotime("0000-$month_1"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_1">
<?php foreach(range(1,31)as $day_1): ?>
<option value="<?php echo $day_1;?>" <?php selectedIf($day_1,@$_REQUEST['day_1']);?>><?php echo $day_1;?></option>
<?php endforeach ?>
</select> Year:
<select name="year_1">
<?php foreach (range($lowestYear,$highestYear) as $year_1):?>
<option value="<?php echo $year_1;?>" <?php selectedIf($year_1,@$_REQUEST['year_1']);?>><?php echo $year_1;?></option>
<?php endforeach?>
</select>
</tr>
<tr>
<td>Date of Birth - Student 2</td>
<td><?php
//pre-populate date fields from the database
//get most current $CURRENT_USER
$CURRENT_USER = _websiteLogin_getCurrentUser();
$_REQUEST['month_2']=date("n",strtotime($CURRENT_USER['student_2_dob']));
$_REQUEST['day_2']=date("j",strtotime($CURRENT_USER['student_2_dob']));
$_REQUEST['year_2']=date("Y",strtotime($CURRENT_USER['student_2_dob']));
?>
Month:
<select name="month_2">
<?php foreach(range(1,12) as $month_2): ?>
<option value="<?php echo $month_2;?>" <?php selectedIf($month_2,@$_REQUEST['month_2']);?>><?php echo date("F",strtotime("0000-$month_2"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_2">
<?php foreach(range(1,31)as $day_2): ?>
<option value="<?php echo $day_2;?>" <?php selectedIf($day_2,@$_REQUEST['day_2']);?>><?php echo $day_2;?></option>
<?php endforeach ?>
</select> Year:
<select name="year_2">
<?php foreach (range($lowestYear,$highestYear) as $year_2):?>
<option value="<?php echo $year_2;?>" <?php selectedIf($year_2,@$_REQUEST['year_2']);?>><?php echo $year_2;?></option>
<?php endforeach?>
</select>
</tr>
And,
<tr>
<td>Date of Birth - Student 3</td>
<td><?php
//pre-populate date fields from the database
//get most current $CURRENT_USER
$CURRENT_USER = _websiteLogin_getCurrentUser();
$_REQUEST['month_3']=date("n",strtotime($CURRENT_USER['student_3_dob']));
$_REQUEST['day_3']=date("j",strtotime($CURRENT_USER['student_3_dob']));
$_REQUEST['year_3']=date("Y",strtotime($CURRENT_USER['student_3_dob']));
?>
Month:
<select name="month_3">
<?php foreach(range(1,12) as $month_3): ?>
<option value="<?php echo $month_3;?>" <?php selectedIf($month_3,@$_REQUEST['month_3']);?>><?php echo date("F",strtotime("0000-$month_3"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_3">
<?php foreach(range(1,31)as $day_3): ?>
<option value="<?php echo $day_3;?>" <?php selectedIf($day_3,@$_REQUEST['day_3']);?>><?php echo $day_3;?></option>
<?php endforeach ?>
</select> Year:
<select name="year_3">
<?php foreach (range($lowestYear,$highestYear) as $year_3):?>
<option value="<?php echo $year_3;?>" <?php selectedIf($year_3,@$_REQUEST['year_3']);?>><?php echo $year_3;?></option>
<?php endforeach?>
</select>
</tr>
The first CMS Builder reference book is now available on-line!
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php