php - How do I order an array using a calculated value, where the rest of the data is received from mysql database? -


i have page called view.htm displays results according user assigns gold medals, silver medals , bronze medals , whether select use gdp or population calculate score view.php. need find way order these results calculated score , limit top 10. data received mysql database , score calculated in php page. using json_encode encode data display on page. $results is two-dimensional array. results when echo out view.php:

{"gold":"0","silver":"0","bronze":"1","gdp":"20343461030","population":"34385000",   "country_name":"afghanistan","score":"0.029082448742184"},{"gold":"0",   "silver":"0","bronze":"0","gdp":"12959563902","population":"3205000",   "country_name":"albania","score":"0"},{"gold":"1","silver":"0","bronze":"0",   "gdp":"188681000000","population":"35468000","country_name":"algeria",   "score":"0.14097214390436"} 

below php code used calculate score:

$results = array();  while ($row = $res->fetchrow()){     $resgold = $row['gold'];     $ressilver = $row['silver'];     $resbronze = $row['bronze'];     $resgdp = $row['gdp'];     $respopulation = $row['population'];     $rescountry = $row['country_name'];      $gold_score = ($resgold * $gold_value);     $silver_score = ($ressilver * $silver_value);     $bronze_score = ($resbronze * $bronze_value);      $total_medals = ($resgold + $ressilver + $resgold);     $permillion = $respopulation/1000000;     $perbillion = $resgdp/1000000000;      $score_pop = (($gold_score + $silver_score + $bronze_score)/$permillion);     $score_gdp = ($perbillion/($gold_score + $silver_score + $bronze_score + 1));      if($population == 'true'){          $row['score'] = "$score_pop";         array_push($results,$row);     }      else if($gdp == 'true'){          $row['score'] = "$score_gdp";          array_push($results,$row);     } }   

any appreciated. thanks

i try calculate score on database side , limit there.

select gold,silver,bronze,gdp,population,country_name,(gold * $gold_value + silver * $silver_value + bronze * $bronze_value) / (population / 1000000) score table sort score desc limit 0,10

or

select gold,silver,bronze,gdp,population,country_name,(gdp / 1000000000) / (gold * $gold_value + silver * $silver_value + bronze * $bronze_value + 1) score table sort score desc limit 0,10

depending on population , gdp values. in case should careful amount of information table has, , if performance starts degrade, may worth keep separate column pre-calculated score, updated when rest of information updated.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -