1

guys. I have the following script

<?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>";
?>

This script is showing all the content from the MySQL table, but the rows are mixed, and as I'm using it for a rang list, I would like to show the rows, filtered by points. The row with the higher point to be first and so on. Thank you in advance, I've not done something. like that before, so I've got no idea, how to make it work.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
user215584
  • 105
  • 7

5 Answers5

1

You need to sorting of result by points ... in desc order

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

alwaysLearn
  • 6,882
  • 7
  • 39
  • 67
1

User ORDER BY in your query For higher points first.

Try this...

$result = mysql_query("SELECT * FROM players ORDER BY points DESC");
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
1

In SQL there is an option as ORDER BY You can use that for this kind of situations.

For Max to Min,

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

For Min to Max,

$result = mysql_query("SELECT * FROM players ORDER BY points ASC") ;
Vinoth Babu
  • 6,724
  • 10
  • 36
  • 55
1

You have to edit your mysql qyuery as

SELECT *
FROM players
ORDER BY points DESC 
Balaji
  • 345
  • 2
  • 8
1

Use ORDER BY clause in your query.

SELECT * FROM players ORDER BY points DESC

Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • I tried with points, but it is still mixing them, So I added an echo 'Enter the players with the less points first' and ordered it by id, so it is now ok. This is also an alternative. – user215584 Mar 27 '13 at 07:58
  • 1
    It should work may be something must be going wrong. Anyways hope you get your problem solved. – Rikesh Mar 27 '13 at 07:59