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

时间:2014-08-26 18:40:18

标签: php dynamic

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

用户从数据表中选择一条记录并单击编辑。例如,用户选择了以灰色突出显示的行,然后单击将其发送到编辑表单的编辑 enter image description here

编辑表单:我将单元ID传递给编辑表单,并根据传递的id预先填充输入字段,但这里是我无法填充和设置默认下拉选项值的地方 enter image description here

以下是下拉选择(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>";
            }
          }

我可以在php while语句中使用if / else语句吗?这可能就是为什么我没有在下拉列表中获得任何数据结果?

1 个答案:

答案 0 :(得分:1)

我明白了:

我需要使用内联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>