Browse Source

Added ranking to ricehalla api results

pull/24/head
Teknikode 5 years ago
parent
commit
90561fb836
2 changed files with 40 additions and 0 deletions
  1. 16
    0
      api/api.php
  2. 24
    0
      includes/common.php

+ 16
- 0
api/api.php View File

@@ -232,10 +232,26 @@ if (isset($_GET['component']))
}
array_push($result_list, $result);
}
// Generate Ranking List
$rankResults = $db->select_raw('ricehalla', "INNER JOIN votes ON ricehalla.id=votes.row_id WHERE votes.table_name=? GROUP BY votes.row_id ORDER BY TotalRank DESC, TotalVotes DESC, TotalPoints DESC", array("ricehalla"), 'ricehalla.id, sum(votes.points) as TotalPoints, COUNT(votes.id) as TotalVotes, (sum(votes.points) / COUNT(votes.id)) * abs(sum(votes.points)) as TotalRank');
$rank_list = array();
foreach ($rankResults as $rank_result)
{
if (!is_array($rank_result))
{
$result_list = array($rankResults);
break;
}
array_push($rank_list, $rank_result);
}
$result_array = array();
// Generate object for each result
foreach ($result_list as $result)
{
$id = $result['id'];
$rank = multi_array_search($rank_list, array('id' => $id))[0];
$username = $userTools->get($result['user_id'])->username;
$image_src = $result['url'];
$date_posted = $result['date_added'];

+ 24
- 0
includes/common.php View File

@@ -633,4 +633,28 @@ function is_dir_empty($dir)
}
return TRUE;
}
function multi_array_search($array, $search)
{
// Create the result array
$result = array();
// Iterate over each array element
foreach ($array as $key => $value)
{
// Iterate over each search condition
foreach ($search as $k => $v)
{
// If the array element does not meet the search condition then continue to the next element
if (!isset($value[$k]) || $value[$k] != $v)
{
continue 2;
}
}
// Add the array element's key to the result array
$result[] = $key;
}
// Return the result array
return $result;
}
?>

Loading…
Cancel
Save