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

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题