I have 2 mysql queries, one to get all the my friends and the other to get the details and a sum of the points for each of my friends.
$get_my_friends_query = mysqli_query($GLOBALS['connect'], "SELECT friend_user_id FROM tbl_friend_link WHERE tbl_friend_link.your_user_id = 1");
while($get_my_friends_result = mysqli_fetch_assoc($get_my_friends_query))
{
$get_friend_user_details_query = mysqli_query($GLOBALS['connect'], "SELECT tbl_registered_user.user_id, tbl_registered_user.first_name, tbl_registered_user.last_name, tbl_registered_user.profile_name, SUM(tbl_point.amount) FROM tbl_registered_user JOIN tbl_point ON tbl_registered_user.user_id = tbl_point.user_id WHERE tbl_registered_user.status = 1 AND tbl_registered_user.user_id = '" . $get_my_friends_result['friend_user_id'] ."' GROUP BY tbl_registered_user.user_id ORDER BY SUM(tbl_point.amount)");
$get_friend_user_details_result = mysqli_fetch_assoc($get_friend_user_details_query);
$result = array(
"done" => "true",
"name" => $get_friend_user_details_result['first_name'] . " " . $get_friend_user_details_result['last_name'],
"profile_name" => $get_friend_user_details_result['profile_name'],
"userID" => $get_friend_user_details_result['user_id'],
"points" => $get_friend_user_details_result['SUM(tbl_point.amount)'],
"number_friends" => mysqli_num_rows($get_my_friends_query),
"error_message" => ""
);
$row_array[] = $result;
}
echo json_encode($row_array);
I am using the first query because I would like a result to be display regardless of whether they have any points or not. I would like to sort the results based on the SUM of the points however that information is being retrieved in the second query however I could not find a suitable explanation on how to use usort on the $row_array.