2013-03-27 30 views
1

夥計。我有以下腳本如何按點過濾行。 PHP/MySQL

<?php 
$result = mysql_query("SELECT * FROM players") or die(mysql_error()); 
echo "<table class=\"table table-bordered table-hover\" border='1' cellpadding='10'>"; 
echo "<tr> <th>ID</th> <th>Place</th> <th>Name</th> <th>Points</th> <th></th> <th></th></tr>"; 

// loop through results of database query, displaying them in the table 
while($row = mysql_fetch_array($result)) { 

    // echo out the contents of each row into a table 
    echo "<tr>"; 
    echo '<td>' . $row['id'] . '</td>'; 
    echo '<td>' . $row['place'] . '</td>'; 
    echo '<td>' . $row['name'] . '</td>'; 
    echo '<td>' . $row['points'] . '</td>'; 
    echo '<td><a href="wtawomenedit.php?id=' . $row['id'] . '">Edit</a></td>'; 
    echo '<td><a href="deleter.php?id=' . $row['id'] . '">Delete</a></td>'; 
    echo "</tr>"; 
} 

// close table> 
echo "</table>"; 
?> 

此腳本顯示從MySQL表中的所有內容,但行混合,因爲我在使用它響了名單,我想顯示的行,按點過濾。具有較高點的那一行是第一個等等。先謝謝你,我沒有做過什麼。像之前那樣,所以我不知道如何使它工作。

回答

1

使用ORDER BY子句中查詢。

SELECT * FROM players ORDER BY points DESC 

注:Please, don't use mysql_* functions in new code。他們不再維護and are officially deprecated。請參閱red box?請改爲了解prepared statements,並使用PDOMySQLi - this article將幫助您決定哪個。如果您選擇PDO,here is a good tutorial

+0

我嘗試過積分,但它仍然在混合它們,所以我添加了一個回聲'首先輸入少於點數的球員',然後排序它由id,所以現在好了。這也是一種選擇。 – user215584 2013-03-27 07:58:18

+1

它應該工作可能是一定會出錯的。反正希望你能解決你的問題。 – Rikesh 2013-03-27 07:59:55

1

您需要由點結果的排序......在倒序

$result = mysql_query("SELECT * FROM players ORDER BY points DESC");

+0

看到這個http://www.w3schools.com/sql/sql_orderby.asp – alwaysLearn 2013-03-27 07:51:40

1

用戶ORDER BY在您的查詢先選擇高一點。

嘗試......

$result = mysql_query("SELECT * FROM players ORDER BY points DESC"); 
1

在SQL中有一個選項爲ORDER BY您可以使用該種情況。

對於最大到最小,

$result = mysql_query("SELECT * FROM players ORDER BY points DESC") ; 

對於最小至最大,

$result = mysql_query("SELECT * FROM players ORDER BY points ASC") ; 
1

您可以編輯你的MySQL qyuery作爲

SELECT * 
FROM players 
ORDER BY points DESC