2012-05-15 42 views
0

我已經做了一個過濾器功能,但我遇到了啓動表的問題,我希望顯示從表中選擇的所有數據,如果沒有爲過濾器選擇一個類別。到目前爲止,如果選擇一個類別,但只有選擇一個類別,纔可以顯示所需的所有必要數據。如果沒有選擇,它會顯示一個空白表。請幫助,...提交按鈕觸發下拉列表過濾器

這裏的功能

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(); 
    ?> 

回答

0

剛如果條件爲查詢

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; 
    } 
} 
+0

yey!它現在工作正常!謝謝! ^^ – ayou

0

你需要檢查出$濾空

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"; 
} 
相關問題