duanlongnao0028 2014-08-26 18:40
浏览 334
已采纳

如何在动态下拉列表中设置默认值

I have a drop down select populated dynamically from a MySQL database and I need to default the drop down value to display based on which record the user selected. What I have so far is not displaying any values in the drop down. But I have code that is outside of the select option statement and it displays the defaulted value in bold. I just can't seem to get the drop down to walk through the if statement logic.

A user selects a record from the data table and clicks edit. For example the user selected a row that is highlighted in grey then clicks edit that sends them to an edit form enter image description here

Edit form: I pass the unit id to the edit form and pre-populate the input fields based on the passed id but here is where I can't populate and set the default drop down option values enter image description here

Here is the code behind the edit form for the drop down select (div id drop down)

    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. "]");
    }

.......

Dynamic drop down select:

<!-- 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>";
            }
          }

Can I have an if/else statement within the php while statement? Is that maybe why I'm not getting any data results in the drop down?

  • 写回答

1条回答 默认 最新

  • doujiang1001 2014-08-28 19:57
    关注

    I figured it out:

    I needed to use an inline if statement like so:

    <!-- 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料