dpcyx08288 2017-04-12 10:17
浏览 27
已采纳

PHP打印下拉列表,默认情况下从中间选择

I have a dropdown list that works fine, but halfway through the list of 80 values is the one I want as selected by default

$cosOptions = cos_options();

Is the variable

<td><select name="options"><?php print $cosOptions; ?></select></td>

Prints the dropdown, then the function below.

function cos_options()
{
    $dbh = dbh_get();
    $options = '';

    $sql = 'select code, descr FROM cos';
    $stmt = $dbh->prepare($sql);
    $stmt->execute();
    while (true) {
        $r = $stmt->fetch();
        if (is_bool($r)) break;
        $options .= '<option value="' . $r['code'] . ' ' . $r['descr'] . '">' . $r['code'] . ' ' . $r['descr'] . '</option>';
    }

    dbh_free($dbh);
    return $options;
}

The Postgres table only has two columns and 80 rows

code descr

D1 Pizza

D2 Bread

D3 Cornflakes etc,.

What I need is D43 Rice to be selected at first because 90% of the time it will be this. How can I achieve this? I've gone through a bunch of posts on here but can't get it working.

  • 写回答

3条回答 默认 最新

  • dongpu4141 2017-04-12 10:27
    关注
    function cos_options()
    {
    $dbh = dbh_get();
    $options = '';
    
    $sql = 'select code, descr FROM cos';
    $stmt = $dbh->prepare($sql);
    $stmt->execute();
    while (true) {
        $r = $stmt->fetch();
        if (is_bool($r)) break;
        $temp=$r['code']. ' '. $r['descr'];
        if($temp=="D43 Rice") //check for whatever you want to set selected
        {
        $options .= '<option selected value="' . $r['code'] . ' ' . $r['descr'] . '">' . $r['code'] . ' ' . $r['descr'] . '</option>';
        }
        else
        {
        $options .= '<option value="' . $r['code'] . ' ' . $r['descr'] . '">' . $r['code'] . ' ' . $r['descr'] . '</option>';
        } 
    }
    
    dbh_free($dbh);
    return $options;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛