当我以递减顺序显示行时,选定的行不显示

时间:2018-03-14 12:30:19

标签: php

enter image description here

背景

当用户在下拉菜单中选择其中一个选项[例如:Undelivered]时,我只显示具有该值的行[example:Undelivered]

enter image description here

t=# with c(t) as (values(now()),(date_trunc('hour',now())))
select replace(to_char(t,'fmHH12:MI am'),':00 ',' ') from c;
 replace
----------
 12:29 pm
 12 pm
(2 rows)

脚本

<tr><th>
<select id="my_select" onchange="send_option();">
<option value="all">Status</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>";
?>              
</select>  
</th></tr>

<?php
$theBigQuery = "SELECT * FROM do_order WHERE 1";

        if (isset($_POST['my_option']) && $_POST['my_option'] != "")
        {
            if($_POST['my_option'] == "all")
            {

            } 
            else 
            {
                $theBigQuery .= " AND in_transit like '" . $_POST["my_option"] . "'";
            }
            echo "<script type='text/javascript'>function changeOption(){document.getElementById('my_select').value='".$_POST['my_option']."';} changeOption();</script>";

        } 
$orderrecords = $db_handle->runSelectQuery($theBigQuery);

?>

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

<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 :(得分:1)

使用您编辑,当您选择一个选项时,您的查询将变为:

$theBigQuery = "SELECT * FROM do_order ORDER BY id DESC AND in_transit like 'Undelivered'";

这是一个错误的sql查询!!

所以解决这个问题;保持$theBigQuery不变,在if (isset( ..测试之后,在运行查询之前,添加ORDER BY子句:

$theBigQuery = "SELECT * FROM do_order WHERE 1";

        if (isset($_POST['my_option']) && $_POST['my_option'] != "")
        {
            if($_POST['my_option'] == "all")
            {

            } 
            else 
            {
                $theBigQuery .= " AND in_transit like '" . $_POST["my_option"] . "'";
            }
            echo "<script type='text/javascript'>function changeOption(){document.getElementById('my_select').value='".$_POST['my_option']."';} changeOption();</script>";

        } 
$theBigQuery .= " ORDER BY id DESC";
$orderrecords = $db_handle->runSelectQuery($theBigQuery);