Sorting output data in table BUT with tie-breaking criteria
Posted: Wed Jun 25, 2008 8:12 am
Hey everyone, I am a complete PHP newbie and have been trying to figure out how to do something that I believe is very possible to code (if you know what you're doing).
I am pulling "online racing league" stats from a MySQL DB and generating an HTML table for it. Here is an example I threw together: http://nhrl.net/demo/mytestcode.php
Here is the PHP portion for that page:
What I would like to do is sort each column of stats by clicking on the header / label row. If you keep clicking on the same column header, the descending / ascending action would go back and forth.
Now, I have found many codes that enable you to "sort by column header"," including in this forum, but I can't find an example that shows how to set up multiple levels of tie-breaking criteria. I could achieve this by making a million different .php files and setting their initial ORDER differently, but that's far, far from ideal.
I made another example page showing exactly what my table would need to do: http://nhrl.net/demo/functionality4.php
If you notice, when there's a tie in a stat, there are particular criteria that determine who gains the advantage in a tie break. For example, when you sort Wins in descending order, there are 2 tie breaks: 1) least number of races and 2) highest point total. When you sort Wins in ascending order, the "races" and "points" criteria then go in the other direction.
Obviously, there are many different combinations of tie breaks for other columns of stats that I'm not showing in these examples. I was wondering if anyone could help me get started with an example code showing how to achieve this type of sorting with (let's suppose) 2 levels of tie-breaking criteria.
Thanks for any guidance you can provide!
I am pulling "online racing league" stats from a MySQL DB and generating an HTML table for it. Here is an example I threw together: http://nhrl.net/demo/mytestcode.php
Here is the PHP portion for that page:
Code: Select all
<?php
include '../test/authentication/connection.php'; //login info
include '../test/authentication/open.php'; //open db code
$result = mysql_query("SELECT Driver, Races, Wins, Points FROM Demo ORDER BY Points DESC");
echo "<table width='300' cellspacing='0' cellpadding='0' class='basictable'>
<tr>
<th width='150'>Driver</th>
<th width='60'>Races</th>
<th width='60'>Wins</th>
<th width='75'>Wins</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Driver'] . "</td>";
echo "<td>" . $row['Races'] . "</td>";
echo "<td>" . $row['Wins'] . "</td>";
echo "<td>" . $row['Points'] . "</td>";
echo "</tr>";
}
echo "</table>";
include '../test/authentication/close.php'; //close db code
?>Now, I have found many codes that enable you to "sort by column header"," including in this forum, but I can't find an example that shows how to set up multiple levels of tie-breaking criteria. I could achieve this by making a million different .php files and setting their initial ORDER differently, but that's far, far from ideal.
I made another example page showing exactly what my table would need to do: http://nhrl.net/demo/functionality4.php
If you notice, when there's a tie in a stat, there are particular criteria that determine who gains the advantage in a tie break. For example, when you sort Wins in descending order, there are 2 tie breaks: 1) least number of races and 2) highest point total. When you sort Wins in ascending order, the "races" and "points" criteria then go in the other direction.
Obviously, there are many different combinations of tie breaks for other columns of stats that I'm not showing in these examples. I was wondering if anyone could help me get started with an example code showing how to achieve this type of sorting with (let's suppose) 2 levels of tie-breaking criteria.
Thanks for any guidance you can provide!