我已经制作了一个过滤器功能,但是我在使用起始表时遇到问题,如果没有为过滤器选择类别,我希望显示从表中选择的所有数据。到目前为止,我可以显示选择类别所需的所有必要数据,但前提是选择类别。如果未选择任何选项,则会显示一个空白表。请帮忙,...
这是函数
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();
?>
只需输入 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;
}
}
您需要
签出空$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";
}