繁体   English   中英

如何在动态下拉菜单中设置默认值

[英]How to set the default value in a dynamic drop down

我有一个从MySQL database动态填充的drop down select ,我需要默认下拉值以根据用户选择的记录显示。 到目前为止,我没有在下拉列表中显示任何值。 但是我的代码在select选项语句之外,并且以粗体显示默认值。 我似乎无法理解if语句逻辑。

用户从数据表中选择一条记录,然后单击“编辑”。 例如,用户选择了以灰色突出显示的行,然后单击“ edit”(将其发送到编辑表单) 在此处输入图片说明

编辑表单:我将单位ID传递给编辑表单,并根据传递的ID预填充输入字段,但是在这里我无法填充并设置默认的下拉选项值 在此处输入图片说明

这是下拉选择(div id下拉)的编辑表单后面的代码

    define('DBHOST','localhost');
    define('DBUSER','root');
    define('DBPASS','*********');
    define('DBNAME','fdmamaint');

    if (!$db = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME)) die("Can't connect to database");

 // Retrieve the record the user selected
    $id = htmlspecialchars($_GET["id"]); 

// Query the depunits table based on the user selection 
    $sql = "SELECT unit_id, div_id, title_org, short_desc, long_desc, unit_desc, avail_ind "
            . "FROM depunits "
            . "WHERE unit_id=$id";
    if (!$result = $db->query($sql)){
            die("There was an error running the query [" .$db->error. "]");
    }

// Walk through the result set and assign variables for reference later on in the script
    while ($row = $result->fetch_assoc()){
        $unitId = $row['unit_id'];
        $divId = $row['div_id'];
        $titleOrg = $row['title_org'];
        $shortDesc = $row['short_desc'];
        $unitDesc = $row['unit_desc'];
        $longDesc = $row['long_desc'];
        $enabled = $row['avail_ind'];
    }

  // Div Id - query database to get drop down select options
    $divSelect = "SELECT div_id, long_desc FROM depdivisions "
            . " WHERE div_id <> '' and avail_ind='Y' and active_ind='Y'"
            . " ORDER BY div_id";
    if (!$divResult = $db->query($divSelect)){
            die("There was an error running the query [" .$db->error. "]");
    }

.......

动态下拉选择:

<!-- Div Id -->                                        
<label for="divId" class="control-label">Div Id</label>
   <select class="form-control" name="divId" id="divId">
       <option value=" "></option>
       <?php
          while ($rowDivs = $divResult->fetch_assoc()) 
         {
            $data = '37' .$rowDivs['div_id'];

            // if the value of $data matches the value of div id of the 
            // user selected row in the database then give that option 
            // value the selected tag                                      
            if ( $data == $divId) 
            {
               echo "<option value=". $data ."selected>";
               echo $data ;
               echo "</option>";
            }
            else 
            {
               echo "<option value=". $data .">";
               echo $data;
               echo "</option>";
            }
          }

我可以在while语句中有if / else语句吗? 这也许就是为什么我没有在下拉菜单中得到任何数据结果的原因吗?

我想到了:

我需要使用内联if语句,如下所示:

<!-- Div Id -->                                        
<label for="divId" class="control-label">Div Id</label>
   <select class="form-control" name="divId" id="divId">
       <option value=" "></option>
       <?php
          while ($rowDivs = $divResult->fetch_assoc()) {

              $data = '37' .$rowDivs['div_id'];
              echo "<option value=$data" .($data == $divId ? ' selected="selected"' : '') . ">$data</option>";  
          }     
       ?>
   </select>

暂无
暂无

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

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