How to make the highest number show up? php?

Pierre S

New member
OK, I am making a most popular games html table, the php keeps tracks of how many clicks, so how would I make the most clicks show up first, then the 2nd then the 3rd, down to 5th.

Please help, thanks, if possible in html and php only.
I mean so it shows the page with the most clicks. I have it tracked already. Just need to find out how to make it so that the page in my db with the most clicks shows up first.
 
You put your data in a hash table (count, name). Then you do a simple "bubble sort". I do not currently have PHP on Ubuntu right now (I just realized when trying to write your example). I am installing it now. Once I get it up and no one has given you an example yet I will edit this answer and give you the code. :)

Edit: I got this working. I do not know how you are storing your information. I am assuming in a hash table.

<?php

// Load data into hash table
$list[0]["click_count"] = 5;
$list[0]["game_name"] = 'Starcraft';

$list[1]["click_count"] = 4;
$list[1]["game_name"] = 'Diablo';

$list[2]["click_count"] = 8;
$list[2]["game_name"] = 'World of Warcraft';

$list[3]["click_count"] = 2;
$list[3]["game_name"] = 'Torchlight';

// Bubble sort
for ($i = 0; $i < sizeof($list); $i++)
{
for ($j = 0; $j < sizeof($list); $j++)
{
if ($list[$i]["click_count"] > $list[$j]["click_count"])
{
$hold = $list[$i];
$list[$i] = $list[$j];
$list[$j] = $hold;
}
}
}

// print hash table
?>

<html>
<body>
<table>

<?php foreach ($list as $value) { ?>
<tr>
<td><?php echo $value["click_count"]; ?></td>
<td><?php echo $value["game_name"]; ?></td>
</tr>
<?php } ?>

</table>
</body>
</html>
 
Back
Top