下拉列表过滤器由提交按钮触发

时间:2012-05-15 06:22:31

标签: php html-table edit

我已经创建了一个过滤器功能但是我遇到了起始表的问题,如果没有为过滤器选择类别,我希望显示从表中选择的所有数据。到目前为止,如果选择了一个类别,我可以显示所需的所有必要数据,但只有选择了一个类别。如果未选择,则显示空白表。请帮助,...

这是函数

 function listhistoryHost(){
    $accountid=$_SESSION['userid'];
    if(isset($_POST['button'])){

    $filter=$_POST['historyfilter'];

    if($filter==$filter){
    $sql="select * from webhostrequest where status='$filter' order by webhostrequest.recentact desc";
    $result=mysql_query($sql) or die("Error in selecting items ".mysql_error());
        $out="<ul>";
            while($row=mysql_fetch_array($result)){

                $accountid=$row['userid'];
                $requesttitle=$row['requesttitle'];
                $requestid=$row['requestid'];

                echo "<table width=\"100%\" border=\"0\" cellpadding=\"2\">";
                echo "<tr class=\"rowcolor1\">";
                    echo "<td width=\"70%\"><span style=\"padding-left:20px\">Requested Web Hosting for  ".$row["requesttitle"]. "</td>";
                    echo "<td width=\"20%\"><span style=\"padding-left:20px\">Status: " .$row["status"]. "</td>";
                    echo "<td>
                    <center>
                        <form id = \"form1\" method = \"post\" action = \"viewhistorywebhost.php?webhost=$requestid\">
                            <input type = \"submit\" name = \"button\" id = \"button\" value = \"Details\" />
                        </form>
                    </center>";
                echo "</tr>";   
            }
            echo "</table>";
    return $out;
    }
    }
    }

这是表单和触发器

 <form id = "form1" method = "post" action = "#">
        <select id="select1" name="historyfilter">
        <option value='' selected disabled>Select item type</option>
            <?php
                $options = array('approved' => 'Approved',
                                'cancelled'=>'Cancelled',
                                'rejected' => 'Rejected');
                foreach($options as $value => $type){
                    echo "<option value=\"$value\">$type</option>";
                }
            ?>
        </select>   
            <input type = "submit" name = "button" id = "submit" value = "Go" />
        </form>
    <?php
        $webhost=new requisition2();
        echo $webhost->listhistoryHost();
    ?>

2 个答案:

答案 0 :(得分:0)

您需要查看$ filter的空白

if (empty($filter)) {
    $sql="select * from webhostrequest order by webhostrequest.recentact desc";
} else {
    $sql="select * from webhostrequest where status='$filter' order by webhostrequest.recentact desc";
}

答案 1 :(得分:0)

只需将if条件放入查询

function listhistoryHost(){
    $accountid=$_SESSION['userid'];
    if(isset($_POST['button'])){

    $filter=$_POST['historyfilter'];
    $sql="select * from webhostrequest order by webhostrequest.recentact desc";
    if(isset($filter))
       $sql="select * from webhostrequest where status='$filter' order by webhostrequest.recentact desc";

    $result=mysql_query($sql) or die("Error in selecting items ".mysql_error());
        $out="<ul>";
            while($row=mysql_fetch_array($result)){

                $accountid=$row['userid'];
                $requesttitle=$row['requesttitle'];
                $requestid=$row['requestid'];

                echo "<table width=\"100%\" border=\"0\" cellpadding=\"2\">";
                echo "<tr class=\"rowcolor1\">";
                    echo "<td width=\"70%\"><span style=\"padding-left:20px\">Requested Web Hosting for  ".$row["requesttitle"]. "</td>";
                    echo "<td width=\"20%\"><span style=\"padding-left:20px\">Status: " .$row["status"]. "</td>";
                    echo "<td>
                    <center>
                        <form id = \"form1\" method = \"post\" action = \"viewhistorywebhost.php?webhost=$requestid\">
                            <input type = \"submit\" name = \"button\" id = \"button\" value = \"Details\" />
                        </form>
                    </center>";
                echo "</tr>";   
            }
            echo "</table>";
    return $out;
    }
}