当我们从下拉列表中选择选项时,显示所选行而不是所有行

时间:2018-03-09 10:23:08

标签: php

我从数据库&获取值用下面的代码显示在表格中。

enter image description here

<tr>                        
    <th>ORDERID</th>              
    <th>STATUS</th>             
</tr>                    
<tr>               
    <td><?php echo $orderrecords[$k]["order_id"]; ?></td>               
    <td><?php echo $orderrecords[$k]["in_transit"]; ?></td>     
</tr>

要求

下拉框的值是从表头中的数据库中获取的。当用户在下拉菜单中选择其中一个选项[例如:未送达]时,我只需要显示具有该值的行[例如:未送达]

enter image description here

问题

我尝试下面的代码,现在当用户选择&#34; Undelivered&#34;时,它不会过滤具有值&#34; Undelivered&#34;的行,而是显示表中的所有行并显示值为&#34;未发送&#34;对于Column。下面的所有表格行。

enter image description here

<table>         
<tr>                        
    <th>ORDERID</th>              
    <th>

    <select id="my_select" onchange="send_option();">
      <option>Select an option</option>     
<?php

$query = "SELECT DISTINCT in_transit FROM do_order";
$result = mysqli_query ($mysqli, $query);
while ( $row = mysqli_fetch_array($result) )
  echo "<option value='" . $row['in_transit'] . "'>" . $row['in_transit'] . "</option>";
?>                                
    </th> 

</tr>

<?php

if(!empty($orderrecords)) 
  {
    foreach($orderrecords as $k=>$v) 
       {                            
?>

<tr>             
<td><?php echo $orderrecords[$k]["order_id"]; ?></td>               
<td>
<?php

if (isset($_POST['my_option'])) 
{ 
$query = "SELECT DISTINCT in_transit FROM do_order where in_transit like '" . $_POST["my_option"] . "'";
} 

$result = mysqli_query ($mysqli, $query);

while( $row = mysqli_fetch_array($result) )
  echo $row['in_transit']."<br>";

?>

</td>   
</tr>

<?php 
    $tabindex++;
  }
     }
?>

</table> 

<form method="post" action"dashboard.php" style="display:none" id="my_form">
      <input type="text" id="my_option" name="my_option"/>
</form>


<form method="post" action"dashboard.php" style="display:none" id="my_form">
      <input type="text" id="my_option" name="my_option"/>
</form>

脚本

function send_option () {
var sel = document.getElementById( "my_select" );
var txt = document.getElementById( "my_option" );
txt.value = sel.options[ sel.selectedIndex ].value;
var frm = document.getElementById( "my_form" );
frm.submit();
}

1 个答案:

答案 0 :(得分:2)

当然! 当您提交表单时,您的脚本将获取所有order_id的列表,将其循环,将当前元素追加到表行的第一个<td>,然后测试您是否已提交具体in_transit。这将始终是正确的,因此它将运行过滤查询:

SELECT DISTINCT in_transit FROM do_order where in_transit like '" . $_POST["my_option"] . "'"

无论order_id是什么,第二个<td>始终是选定的$_POST["my_option"]

您正在尝试过滤输出,因此您需要将选定的my_option传递给获取数据的查询。

因此,要解决此问题,您需要重新运行外部查询:

<?php
    if (isset($_POST['my_option'])) 
    {
       $theBigQuery = "SELECT order_id, in_transit FROM do_order where in_transit like '" . $_POST["my_option"] . "'";
    } else {
       $theBigQuery = "SELECT order_id, in_transit FROM do_order";
    }

    $result = mysqli_query ($mysqli, $theBigQuery);
    $orderrecords =  mysqli_fetch_array($result, MYSQLI_BOTH);
    if(!empty($orderrecords)) 
    {
      foreach($orderrecords as $k=>$v) 
      {                            
?>

<tr>             
   <td><?php echo $orderrecords[$k]["order_id"]; ?></td>               
   <td><?php echo $orderrecords[$k]["in_transit"]; ?></td>
</tr>

<?php 
        $tabindex++;
      }
    }
?>