2017-04-11 111 views
1

我有一個使用JOIN連接在一起的表格,我想創建一個帶有提交按鈕的表單,允許最終用戶更改他們希望過濾表格的方式。 排序欄在我的網頁上,但它實際上沒有做任何事情。所以我的問題是我需要做什麼才能讓排序按鈕起作用。使用PHP對下拉表中的SQL查詢進行排序

<?php 


$sort = @$_POST['order']; 
if (($sort)) { // If you Sort it with value of your select options 
$query = "SELECT n.firstname, n.middlename, n.lastname, m.title, f.format, t.price, e.series, t.inventory 
       FROM book_main AS m 
       JOIN book_author AS n ON n.author_id=m.author_id 
       JOIN book_title AS t ON t.title_id=m.title_id 
       JOIN book_format AS f ON f.format_id=m.format_id 
       LEFT JOIN book_series AS e ON e.series_id=m.series_id 
       ORDER BY '".$sort."' ASC"; 



} 
else { // else if you do not pass any value from select option will return this 
    $query = "SELECT n.firstname, n.middlename, n.lastname, m.title, f.format, t.price, e.series, t.inventory 
       FROM book_main AS m 
       JOIN book_author AS n ON n.author_id=m.author_id 
       JOIN book_title AS t ON t.title_id=m.title_id 
       JOIN book_format AS f ON f.format_id=m.format_id 
       LEFT JOIN book_series AS e ON e.series_id=m.series_id 
        ORDER BY n.lastname ASC"; 


$results = mysql_query($query); 
echo $results['order']; 

//can I have a couple points for trying so hard haha... 
} 

?> 

<form name="sort" action="" method="post"> 
<select name="order"> 
    <option value="choose">Make A Selection</option> 
    <option value='lastname'>Authors Last name</option> 
    <option value='price'>Price</option> 
    <option value='inventory'>Inventory</option> 
</select> 
<input type="submit" value="Sort" /> 
</form> 
+1

問一個問題吧。 –

+0

同意。您是否要求我們提交提交按鈕? – NickSentowski

+0

是的,我要求提交按鈕的工作。因爲它目前沒有做任何事 –

回答

0

要確保$那種在你需要如下檢查的選擇選項列出的值。同樣沒有任何迴應的原因是,order不是$ results數組的一部分。

訪問結果的唯一方法是使用其中任何http://php.net/manual/en/function.mysql-result.php

if(in_array($sort, ['lastname', 'price', 'inventory'])) { 
    $query = "SELECT n.firstname, n.middlename, n.lastname, m.title, f.format, t.price, e.series, t.inventory 
       FROM book_main AS m 
       JOIN book_author AS n ON n.author_id=m.author_id 
       JOIN book_title AS t ON t.title_id=m.title_id 
       JOIN book_format AS f ON f.format_id=m.format_id 
       LEFT JOIN book_series AS e ON e.series_id=m.series_id 
       ORDER BY $sort ASC"; 
} else { 
    $query = "SELECT n.firstname, n.middlename, n.lastname, m.title, f.format, t.price, e.series, t.inventory 
       FROM book_main AS m 
       JOIN book_author AS n ON n.author_id=m.author_id 
       JOIN book_title AS t ON t.title_id=m.title_id 
       JOIN book_format AS f ON f.format_id=m.format_id 
       LEFT JOIN book_series AS e ON e.series_id=m.series_id 
        ORDER BY n.lastname ASC"; 
} 
$results = mysql_query($query); 
while ($row = mysql_fetch_assoc($result)) { 
    echo $row['firstname']; 
    echo $row['middlename']; 
    echo $row['lastname']; 
    echo $row['title']; 
    echo $row['format']; 
} 
+0

我試着用你在這裏做的事情,它仍然沒有對我的表進行排序 –

+0

你是否嘗試直接在數據庫上運行你的查詢,是在那裏工作的順序? – swaveg

+0

是的,我可以直接在數據庫上使用訂單,因爲它最初設置爲按姓氏排序作爲默認值 –

相關問題