All, If I've go the following database fields/values:
user_id field_name field_value 1 EventHour1 1 1 EventMinute1 30 1 EventAMPM1 am 1 Event_1 Set Up 1 EventHour2 5 1 EventMinute2 30 1 EventAMPM2 am 1 Event_2 Take Down
Then I have the following HTML/PHP:
<?php
for($i=0;$i<50;$i++){
if ($i%2==0){
echo '<tr bgcolor="#CCCCCC">';
} else {
echo '<tr>';
}
?>
<td><select size="1" id="EventHour<?php echo $i; ?>" name="EventHour<?php echo $i; ?>" >
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select> : <select size="1" name="EventMinute<?php echo $i; ?>" id="EventMinute<?php echo $i; ?>" >
<option>00</option>
<option>05</option>
<option>10</option>
<option>15</option>
<option>20</option>
<option>25</option>
<option>30</option>
<option>35</option>
<option>40</option>
<option>45</option>
<option>50</option>
<option>55</option>
</select>
<select size="1" name="EventAMPM<?php echo $i; ?>" id="EventAMPM<?php echo $i; ?>">
<option>PM</option>
<option>AM</option>
</select></td>
<td><b>
<select size="1" name="Event_<?php echo $i; ?>" id="Event_<?php echo $i; ?>" class="event_selection">
<option>Select Event...</option>
<option value="Set Up">Set Up</option>
<option value="Take Down">Take Down</option>
</select>
</b></td>
<td> Note:
<input type="text" name="Note<?php echo $i; ?>" id="Note<?php echo $i; ?>">
</td>
</tr>
<?php
}
?>
What I'd like to do is set the select values based on the database results. So if the select id is EventHour1 then I'd like to make the select option be 1 and if it was Event_2 then I'd like the select option to be Take Down.
How can I go about doing this with PHP? Any help would be greatly appreciated!
Thanks in advance
Here's the basic idea using an array. Ideally the options would be read from a database instead (the \t
and are just to keep the output HTML format clean when you have to view source to debug - you can also use
<?php echo $i?>
method to output if you prefer):
$curVal = /* lookup curVal from database for event */
echo "<select size='1' id='EventHour$i' name='EventHour$i'>
";
$options = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
foreach ($options as $option) {
if ($curVal == $option)
$selected = " selected='selected'";
else
$selected = "";
echo "\t<option$selected>$option</option>
";
}
echo "</select>
";
Which should render something like this for Event 2, for example:
<select size='1' id='EventHour2' name='EventHour2'>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option selected='selected'>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
Demo: http://ideone.com/BoEfr
I also question your database design. If you are trying to store events in the database, I think you are confusing the presentation concerns (field id's etc), with data storage. Your database just needs to capture the underlying data semantics, which are the details of the events:
event_id | user_id | event_name | hour | minute | am_pm
1 | 1 | Set Up | 1 | 30 | am
2 | 1 | Take Down | 5 | 30 | am
Then when you are displaying the events, you take care of presenting it in a meaningful manner, i.e., in a table format with dropdowns or textboxes for data entry.