so I've been scratching my head about this one for a while and almost there but still getting unexpected results.
So I have a database containing the following data
username | level1 | level2 | level3
user1 | 39.76072 | 79.41869 | 151.2955
user2 | 39.80072 | 80.73846 | 270.6498
user3 | 39.84072 | 80.81845 | 83.41801
user4 | 39.85321 | 80.90525 | 88.31719
user5 | Zero | Zero | Zero
I've been trying to put these into a multi-dimensional array in order from the lowest to the highest but have really been struggling. These are basically level times and I'm trying to create a leaderboard.
I assume the problem is that the level columns are not INT but rather CHAR
I am currently trying the following
function order_by_time($a, $b) {
return $b["Level".$_POST["level"]] < $a["Level".$_POST["level"]] ? 1 : -1;
}
$query = mysqli_query($con, "SELECT userName, Level".$_POST["level"]." FROM table");
$results = array();
$unranked = array();
while($line = mysqli_fetch_array($query, MYSQLI_ASSOC)){
if($line["Level".$_POST["level"]] == "Zero"){
$unranked[] = $line;
} else{
$results[] = $line;
}
}
usort($results, "order_by_time");
$results = array_merge($results, $unranked);
This works for level1 and for level2 but not for level3. Level1 displays as it does in the aboce database example and so does level2 but the problem is so does level3 and that isn't the correct order.
it looks like its something to do with the number of characters before the decimal.
Any advice on how i can resolve this without touching the database? that isn't really an option as its already being used.
I'd really appreciate some help with this :-)