duanpendan8067 2016-04-04 01:54
浏览 25

选项菜单,上次记录为默认值

I have an option menu as follows:

<select name="selCycle" id="selCycle" onChange="formFilter.submit()">
    <option value="%">all cycles</option>
    <?php
    do {
    ?>
        <option value="<?php echo $row_Recordset2['Cycle'] ?>"
        <?php
        if ($varCycle_DetailRS4 == $row_Recordset2['Cycle']) {
            echo 'selected';
        } elseif ($varCycle2_DetailRS4 == $row_Recordset2['Cycle']) {
            echo 'selected';
        } else {
            echo '';
        }
        ?>
        >
        <?php echo $row_Recordset2['Cycle'] ?>
        </option>
        <?php
    } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2));
        $rows = mysql_num_rows($Recordset2);
        if ($rows > 0) {
            mysql_data_seek($Recordset2, 0);
            $row_Recordset2 = mysql_fetch_assoc($Recordset2);
    }
    ?>
</select>

Currently, the default selection is showing all records. I would like for the default to be the latest set of data equal to:

 <?php echo $row_RecordsetCycle['Cycle']; ?>

So option menu would list 1,2,3,4,5 with 5 being the default when the page loads. User can then pick any option available. is set to last record in table with a limit of 1 so it will always echo the last record, which composes the option menu.

Help please. What should I edit so that the one record in

 <?php echo $row_RecordsetCycle['Cycle']; ?> 

is the default or selected option menu when the page loads? Currently, the default just shows all records and is extremely slow to load, hence why I want the latest record to be the default.

  • 写回答

2条回答 默认 最新

  • duandao3265 2016-04-04 03:40
    关注

    Try using end() to get the last array:

    <?php
    $arr =array(1,2,3,4,5);
    $last = end($arr);
    ?>
    <select name="selCycle" id="selCycle" onChange="formFilter.submit()">
        <option value="%">all cycles</option>
        <?php foreach($arr as $key=>$val):
        if(in_array("$last", array($val))==true){
           echo '<option value="" selected="selected">'.$val.'</option>';
           continue;
        }
         echo '<option>'.$val.'</option>';      
    endforeach; ?>    
    </select>
    

    Stop using mysql extension, use pdo or mysqli. Using your question code, it should be:

    $row_Recordset2 = mysql_fetch_assoc($Recordset2);
    $last = end($row_Recordset2);
    
    foreach($row_Recordset2 as $key=>$val){
       //other code
       if(in_array("$last", array($val))==true){
           echo '<option value="" selected="selected">'.$val.'</option>';
           continue;
        }
         echo '<option>'.$val.'</option>'
    }
    

    Working Demo

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100