View Single Post
  #2 (permalink)  
Old 07-23-2008, 03:42 PM
Michael Fesser
 
Posts: n/a
Diggs:
Default Re: Please help with tricky array question

.oO(Brett)

>I have created a page to update an event record and I'm trying to
>populate the drop-down option list for $months with the value stored in
>the record. I have created a $mysqldate array to separate the different
>elements of the date as stored in the table, for insertion into the
>various parts of the form. For brevity sake, I'm not showing all of the
>code.
>
>> $months =
>> array('Jan','Feb','Mar','Apr','May','Jun','Jul','A ug','Sep','Oct','Nov','Dec');
>> $mysqldate = explode('-', $row_getEvents['event_date']);
>> <select name="month" id="month">
>> <?php for ($i=1;$i<=12;$i++) { ?>
>> <option value="<?php echo $i <10 ? '0'.$i : $i; ?>"
>> <?php echo ' selected="selected"'; ?>><?php echo
>> $months[$i-1]; ?> </option>
>> <?php } ?>
>> </select>
>> <label for="day">Date:</label>
>> <input type="text" name="day" id="day" size="2" maxlength="2"
>> value="<?php echo $mysqldate[2]; ?>" />
>> <label for="year">Year:</label>
>> <input type="text" name="year" id="year" size="4"
>> maxlength="4" value="<?php echo $mysqldate[0]; ?>"/>

>
>The problem is I can't figure out how to take the value $mysqldate[1]
>(which is the value for month) and have it display as the default month
>for this record, while still having all months available in the
>drop-down. The code as I have it includes all months, but the default
>is Dec. I have changed <?php echo $months[$i-1]; ?> to this <?php echo
>$months[$mysqldate[1]-1]; ?> and get the correct month as default, but
>that turns all twelve options into the same month, e.g. twelve Aug's. I
>probably need a conditional statement, but I don't know what the
>condition is or should be. So how do I set the record's month as the
>default choice, while preserving the other eleven choices in the
>drop-down list?


Try this for printing the drop-down options (I don't like the many
switchings between PHP and HTML mode, so this is just a single PHP
block), slightly restructured for legibility:

<?php
$months = array(
'Jan', 'Feb', 'Mar', 'Apr',
'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec'
);
$mysqldate = explode('-', $row_getEvents['event_date']);

print "<select name='month' id='month'>\n";
for ($i = 1; $i <= 12; $i++) {
printf("<option value='%u'%s>%s</option>\n",
$i,
$i == $mysqldate[1] ? " selected='selected' : '',
$months[$i-1]
);
}
print "</select>\n";
?>

One note: I've removed the leading zeros in the option values. Working
with leading zeros if the values are in fact purely numeric can be
troublesome. If your form processing script needs those zeros, use this
format string for the printf() function instead:

"<option value='%02u'%s>%s</option>\n"

Another note: You could also use MySQL's DAY(), MONTH() and YEAR()
functions to let the database return the parts of the date. Then you
wouldn't have to explode() it in your script. Usually I find it easier
and more convenient to let the DB already do as much work as possible
and deliver the data in the format that I need.

HTH
Micha
Reply With Quote