dongyin6576 2012-07-03 21:38
浏览 99
已采纳

根据mySQL结果选择Option

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>&nbsp; 
  <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>&nbsp;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

  • 写回答

1条回答 默认 最新

  • duanjingsen7904 2012-07-03 22:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大