2016-07-30 67 views
-2

我想嘗試在我的網頁上添加兩個過濾器。使用單選按鈕過濾數據

  • 一個日期過濾器和另一個過濾器的顏色。
  • 如果未設置過濾器,則應顯示所有記錄。

我想使用if/else語句以及名稱/值對。

任何人都可以看到我做錯了什麼?我嘗試過幾件事情,而這正是我所結束的。我也讀過很多頁面,並嘗試使用這些例子,但似乎沒有任何工作適合我。 Stackoverflow linkStackoverflow link


HTML/PHP

<?php 
//connect to the database 
$dbc = mysqli_connect('host', 'user', 'password', 'cars') or die('Error connecting to MySQL Server.'); 

//If RadioButton Clicked Sort the Database by dateadded Asc/Desc 
if(isset($_POST['dateorder'])){ 
     if($_POST['dateorder'] == 'dateasc'){ 
      //Run query for dateasc 
      $query = "SELECT * FROM cardetails ORDER BY caradded asc"; 
     }elseif($_POST['dateorder'] == 'datedesc'){ 
      //Run query for datedesc 
      $query = "SELECT * FROM cardetails ORDER BY caradded desc"; 
     } 
}else{ 
     $query = "SELECT * FROM cardetails ORDER BY id asc"; 
} 

//If RadioButton Clicked Sort the Database by Color Red, Green, Blue 
if(isset($_POST['color'])){ 
     if($_POST['color'] == 'red'){ 
      //Run query for red color 
      $query = "SELECT * FROM cardetails WHERE color = 'red'"; 
     }elseif($_POST['color'] == 'green'){ 
      //Run query for green color 
      $query = "SELECT * FROM cardetails WHERE color = 'green'"; 
     }elseif($_POST['color'] == 'blue'){ 
      //Run query for blue color 
      $query = "SELECT * FROM cardetails WHERE color = 'blue'"; 
     } 
}else{ 
    $query = "SELECT * FROM cardetails ORDER BY id asc"; 
} 


$result = mysqli_query($dbc, $query) or die('Error Refreshing the page: ' . mysqli_error($dbc)); 

//Retrieve the practice tasks from the database table 
$result = mysqli_query($dbc, $query) or die('Error querying database.'); 

//start pagination 
//Get the total count of rows. 
$sql = "SELECT COUNT(id) FROM cardetails"; 
$query = mysqli_query($dbc, $sql); 
$row = mysqli_fetch_row($query); 

//Here we have the total row count 
$rows = $row[0]; 

//Number of results to show per page 
$page_rows = 5; 

//This tells us the page number of our last page 
$last = ceil($rows/$page_rows); 

//This makes sure last cannot be less than 1 
if($last < 1){ 
    $last = 1; 
} 
//establish the $pagenum variable 
$pagenum = 1; 

//Get pagenum from URL vars if it is present, else it is = 1 
if(isset($_GET['pn'])){ 
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']); 
} 

//This makes sure the page number isn't below 1 or more than our $last page 
if($pagenum < 1){ 
    $pagenum = 1; 
} else if($pagenum > $last) { 
    $pagenum = $last; 
} 
//This sets the range of rows to query for the chosen $pagenum 
$limit = 'LIMIT ' . ($pagenum - 1) * $page_rows .',' .$page_rows; 

//This is your query again, it is for grabbing just one page worth of rows by applying the limit variable 
$sql = "SELECT id, caradded, make, color FROM cardetails ORDER BY id DESC $limit"; 
$query = mysqli_query($dbc, $sql); 


//$paginationCrls variable 
$paginationCtrls = ' '; 

//if there is more than 1 page worth of results 
if($last != 1){ 
    /*First we check if we are on page one. If we are then we don't need a link to 
    the previous page or the first page so we do nothing. If we aren't then we generate 
    links to the first page and to the previous page.*/ 

    if($pagenum > 1){ 
     $previous = $pagenum - 1; 
     $paginationCtrls .= '<a href="' . $_SERVER['PHP_SELF']. '?pn=' .$previous. '">&#171; Previous</a> &nbsp; &nbsp; '; 
     //Render clickable number links that should appear on the left of the target page number 
     for($i = $pagenum-4; $i < $pagenum; $i++){ 
      if($i > 0) { 
       $paginationCtrls .= '<a href="' .$_SERVER['PHP_SELF']. '?pn=' . $i .'">'.$i.'</a> &nbsp; &nbsp; '; 
      } 
     } 
    } 
//Render the target page number, but without it being a link 
$paginationCtrls .= ''.$pagenum.' &nbsp; &nbsp; '; 
//Render clickable number links that should appear on the right of the target page number 
for($i = $pagenum+1; $i <= $last; $i++){ 
    $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; &nbsp;'; 
    if($i >= $pagenum+4){ 
     break; 
    } 
} 
//This adds the same as above only checking if if we are on the last page and then generating Next 
if($pagenum != $last){ 
    $next = $pagenum + 1; 
    $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next &#187;</a> '; 
} 
} 
//Finish Pagination 
?> 
<!DOCTYPE html> 
<html> 
<head> 
<title>Basic Page Layout</title> 
<link rel="stylesheet" type="text/css" href="basicstyle.css" /> 
</head> 
<body> 
    <div id="wrapper"> 
     <div id="headerwrap"> 
     <div id="header"> 
      <p>Search/Filter Practice</p> 
     </div> 
     </div> 
     <div id="leftcolumnwrap"> 
     <div id="leftcolumn"> 
       <h2>Trial Filters</h2> 
     <form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"> 
     <p>Filter by Date:</p> 
      <input type="radio" name="dateorder" value="dateasc"><label for="dateasc">A - Z</label><br> 
      <input type="radio" name="dateorder" value="datedesc"><label for="datedesc">Z - A</label><br> 
      <br><hr> 
     <p>Filter by Colour:</p> 
      <input type="radio" name="color" value="red"><label for="red">Red</label><br> 
      <input type="radio" name="color" value="green"><label for="green">Green</label><br> 
      <input type="radio" name="color" value="blue"><label for="blue">Blue</label> 
      <br><br> 
      <input name="submit" type="submit"> 
      <br><br> 
     </div> 
     </div> 
     <div id="contentwrap"> 
     <div id="content"> 
     <p style="text-align:center; font-weight:bold;">My Database Records</p> 
<?php 
echo "<table>"; 
echo "<tr>"; 
echo "<th>Car Added On</th>"; 
echo "<th>Make</th>"; 
echo "<th>Color</th>"; 
echo "</tr>"; 
while($row = mysqli_fetch_array($query)){ 
echo "<tr>"; 
echo "<td>". $row['caradded'] ."</td>"; 
echo "<td>". $row['make'] ."</td>"; 
echo "<td>". $row['color'] ."</td>"; 
echo "</tr>"; 
} 
print '</table><br><br>'; 

//close connection to database 
mysqli_close($dbc); 
?> 
<p id="pagination_controls"><?php echo $paginationCtrls;?><br><br> 
     </div> 
    </div> 
     <div id="footerwrap"> 
     <div id="footer"> 
      <p>July/August 2016</p> 
     </div> 
     </div> 
    </div> 
</body> 
</html> 

MySQL的

CREATE DATABASE cars; 

USE cars; 

CREATE TABLE cardetails(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
caradded datetime NOT NULL DEFAULT NOW(), 
make VARCHAR(50) NOT NULL, 
color VARCHAR(50) NOT NULL); 

DESCRIBE cardetails; 

INSERT INTO cardetails(caradded, make, color) VALUES 
(NOW(), "Toyota", "Red"), 
(NOW(), "VW", "Green"), 
(NOW(), "Nissan", "Blue"), 
(NOW(), "Toyota", "Green"), 
(NOW(), "Toyota", "Green"), 
(NOW(), "VW", "Blue"), 
(NOW(), "VW", "Green"), 
(NOW(), "Nissan", "Blue"), 
(NOW(), "Nissan", "Green"); 

SELECT * FROM cardetails; 
+0

什麼是您的具體問題?什麼不行? –

+0

嗨盧卡,當我點擊無論是asc還是desc單選按鈕,沒有任何反應,我需要更多的代碼,幫助找出答案。 –

+0

@LucaJung更新了我的例子,如果你有任何想法。 –

回答

0

你在你的if語句有兩個開放支架。我在你的源代碼中標記了它。
希望能解決你的問題。

編輯2:
你總是得到默認的SQL查詢時,你只選擇dateorder一個單選按鈕,因爲您覆蓋與第二,如果塊中的dateorder查詢。你必須把第二個塊放到else塊中。 (看下面的代碼)

附加信息:
在這個Strucktur會有一個錯誤,當你選擇一個顏色和日期過濾器的共同點,因爲那麼你總是隻有日期查詢。爲了避免這種錯誤,請撥打所有輸入字段名稱相同

<?php 
if(isset($_POST['dateorder'])){ 
     if($_POST['dateorder'] == 'dateasc'){ 
      //Run query for dateasc 
      $query = "SELECT * FROM cardetails ORDER BY caradded asc"; 
     }elseif($_POST['dateorder'] == 'datedesc'){ 
      //Run query for datedesc 
      $query = "SELECT * FROM cardetails ORDER BY caradded desc"; 
     } 
}else if(isset($_POST['color'])){ 
     if($_POST['color'] == 'red'){ 
      //Run query for red color 
      $query = "SELECT * FROM cardetails WHERE color = 'red'"; 
     }elseif($_POST['color'] == 'green'){ 
      //Run query for green color 
      $query = "SELECT * FROM cardetails WHERE color = 'green'"; 
     }elseif($_POST['color'] == 'blue'){ 
      //Run query for blue color 
      $query = "SELECT * FROM cardetails WHERE color = 'blue'"; 
     } 
}else{ 
    $query = "SELECT * FROM cardetails ORDER BY id asc"; 
} 
?> 

編輯:
,因爲你的if語句之前向您查詢您不使用更新的查詢。之後,您只發送更新後的查詢,但不要將其保存到$ result中。

HTML

<form action="" method="post"> 
    <p>Date:</p> 
    <input id="dateorderasc" type="radio" name="dateorder" onclick="javascript:submit()" value="dateasc"<?php if (isset($_POST['dateorder']) && $_POST['dateorder'] == 'dateasc') echo ' checked="checked"';?> /><label for="dateasc">Newest &rsaquo; Oldest</label> 
    <br> 
    <input id="dateorderdesc" type="radio" name="dateorder" onclick="javascript:submit()" value="datedesc" <?php if (isset($_POST['dateorder']) && $_POST['dateorder'] == 'datedesc') echo ' checked="checked"';?> /><label for="datedesc">Oldest &rsaquo; Newest</label> 
</form> 

PHP

<?php 
//connect 
include ("db.connect.php"); 

//Retrieve the tasks from the database table 
$query = "SELECT * FROM tasks"; 

//If RadioButton Clicked Sort the Database by Date Asc/Desc 
if(isset($_POST['dateorder']) && !empty($_POST['dateorder'])){ 
    if ($_POST['dateorder'] == 'dateasc'){     //HERE 
     $query .= " ORDER BY date ASC"; 
    } 
    if ($_POST['dateorder'] == 'datedesc'){ 
     $query .= " ORDER BY date DESC"; 
    } 
} 
$result = mysqli_query($dbc, $query) or die('Error Refreshing the page: ' . mysqli_error($dbc)); 
include("dbresults.php"); 
?> 
+0

再次感謝,更新,但沒有。我會多玩一下,看看會發生什麼。謝謝。 –

+0

查詢現在在我的演示中更新。你確定你的查詢是正確的嗎? –

+0

我更新了我的答案,你用哪個var來顯示結果?爲了使用更新後的$查詢,我將$ result下推。這是否工作? –