I am trying to sort users of a soccer picks site by total points from highest to lowest. I am about 10 hours into trying to get this sort to work. I have walked trough at least a dozen answers on SO alone, read and read the php manual on USORT, SORT, ARSORT, UASORT and probably other sort options. I have gone over and over my code. I am a complete novice at PHP and am now 3 weeks past my intended deadline with this site. I am at my wits end b/c i feel this should be something VERY simple and i am either 1) missing something super simple or 2) overcomplicating either the solution or my code in general.
For a visual idea of what i am trying to accomplish, so can view the rankings page here: acesoccerpicks [DOT] com/standings.php. It is intended to rank players based on total points from highest to lowest.
This is a personal site for myself and friends so the blown deadline isn't awful, but I would prefer not to miss any more matches b/c of missing rankings functionality.
To sum: among other things, i am trying to sort users based on a calculated points variable ($total_pts) and display users as ranked (highest to lowest) in an html table. I also want to display the user's place ranking as a number (ie 1st, 2nd, 3rd, etc).
My code can be viewed here: http://jsfiddle.net/Craig78/e6jmLgj7/. (snippet) foreach ($users as $user) { // do stuff here
// Calculate total points score
$total_pts = (($cnt_winner * $ct) + ($cnt_score * $cs)); /* + (($champ IF AFTER FINAL WEEK) + ($pwt * # of PERFECT WEEKS on TEAMS) + ($pws * # of PERFECT WEEKS on SCORES) + ($cl * # of weeks as LEADER after 3 consec weeks) - ($cb * # of weeks in the bottom after 3 consecutive weeks) ) */
$ranked_users[$standings_userid] = $total_pts;
}
function compare($a, $b) {
if ($a == $b) {
return 0;
}
return ($b < $a) ? -1 : 1;
}
// sort users
usort($ranked_users, 'compare');
foreach ($ranked_users as $ranked_user) {
print_r($ranked_users);
echo $ranked_user;
// TODO: DETERMINE streaks , if any
echo '<tr>';
echo '<td>' . $user['rank'] . '</td>';
if (isset($_SESSION['user_id'])) {
echo '<td><a href="/team_profile.php?user_id=' . $standings_userid . '?user_team=' . mysqli_real_escape_string($user_team) . '" rel="" title="View ' . $user_team . '\'s profile">' . $user_team . '</a></td>';
}
else {
echo '<td>' . $user_team . '</td>';
}
echo '<td>' . $cnt_winner . '</td>';
echo '<td>' . $cnt_score . '</td>';
echo '<td>(' . $champ_pts . ')</td>';
echo '<td>-</td>';
echo '<td>' . $total_pts . '</td>';
echo '</tr>';
}
I know there are a lot of things i can do to improve my code and fully intend to go back and refactor using MVC, remove mysqli usages, utilize functions etc. But being the novice I am with PHP, my sole goal is to make it "good enough" to launch for a tight knit group of friends to use and enjoy the EPL. So please focus answers on guiding me in getting my users ranked effectively , i REALLY need the help.
as I see, what you want to do is get the list of users, and sort them using the points.
Since you have not reflected the form of your users array in the question, I am reproducing one which I think will reflect what you have.
You can use array multi sort function to sort your array as shown below;
http://php.net/manual/en/function.array-multisort.php
Instead of my hardcoded array, you can produce the same like this.
//this is taken from your code. you have to create $user_name
$organized_users = array();
foreach ($users as $user) {
$total_pts = (($cnt_winner * $ct) + ($cnt_score * $cs));
$organized_users[] = array('user' => $user, 'points' => $total_pts)
}
/*
$users = array(
array('user' => 'nimeshka', 'points' => 40),
array('user' => 'abced', 'points' => 25),
array('user' => 'xxyz', 'points' => 60)
);
*/
foreach($organized_users as $s_user){
$points[] = $s_user['points'];
}
array_multisort($points, SORT_DESC, $organized_users);
echo '<pre>';
print_r($organized_users);
This will sort your array from the highest to the lowest.
Please do let me know if this does make sense.