简体   繁体   English

PHP / MSSQL在下拉列表中显示存储的字段值

[英]PHP/MSSQL Present stored field value in a drop-down list

I use the following code to create a number of drop-down lists with the same values. 我使用以下代码创建多个具有相同值的下拉列表。 The values are brought in from an MSSQL table via a query written elsewhere. 这些值是通过在其他地方编写的查询从MSSQL表中引入的。

<?php
 $select = '';
 while($row = $data->fetch(PDO::FETCH_BOTH))
 {
   $select .= "<option value='".$row['Code']."'>".$row['Code']."</option> ";
 }
 echo "<select name=\"proj1[]\">";
 echo $select;
 echo "</select>";
?>

The user makes his selections, then submits the form and the record is written to the PROJECTS table in the PROJ DB (fields: Proj1, Proj2, Proj3, Proj4). 用户进行选择,然后提交表单,并将记录写入PROJ DB(字段:Proj1,Proj2,Proj3,Proj4)中的PROJECTS表中。 The original drop-down values are held in a separate table (CODES). 原始下拉值保存在单独的表(CODES)中。 When the record is called up in a browser, a prepared SELECT statement runs against PROJECTS to load it. 在浏览器中调用记录时,针对PROJECTS运行准备好的SELECT语句以加载它。 I would like to show the user the drop-down selections he made when the completed form is loaded, ie, the values of Proj1-Proj4 for a given record in PROJECTS. 我想向用户展示他在加载完成的表单时所做的下拉选择,即PROJECTS中给定记录的Proj1-Proj4值。 How can I do this? 我怎样才能做到这一点? I'm not sure where to put my 'option selected'. 我不确定将“选择的选项”放在哪里。

Hope this help 希望这个帮助

 $select = '';
    while($row = $data->fetch(PDO::FETCH_BOTH))
    {
        $select .= "<option value='".$row['Code']."' '".$row['Code'] == $_POST['proj1'] ? ' selected="selected"' : ''."'  >".$row['Code']."</option> ";
    }
    echo "<select name=\"proj1[]\">";
    echo $select;
    echo "</select>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM