Looking for a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
InnerShadow
Forum Commoner
Posts: 37
Joined: Thu Nov 10, 2005 10:44 pm
Location: US

Looking for a function

Post by InnerShadow »

Is there any function that will take variables that are generated by a sql query and return the one that has the highest numerical value?

for instance, i have these variables:

Code: Select all

<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("countries", $conn);

$result = mysql_query("SELECT UnitedStates, GreatBritain, France, Spain, Nicaragua, Hawaii, Panama, Jamaica, Sudan, Libya, India, Algeria, Vietnam, CostaRica, Philippines, China, PuertoRico, Cuba FROM forts WHERE Country = '$ID'") 
  or die(mysql_error()); 
while($row = mysql_fetch_assoc($result))
{
  $USF = $row['UnitedStates'];
  $GRF = $row['GreatBritain'];
  $FRF = $row['France'];
  $SPF = $row['Spain'];
  $NIF = $row['Nicaragua'];
  $HAF = $row['Hawaii'];
  $PAF = $row['Panama'];
  $JAF = $row['Jamaica'];
  $SUF = $row['Sudan'];
  $LIF = $row['Libya'];
  $INF = $row['India'];
  $ALF = $row['Algeria'];
  $VIF = $row['Vietnam'];
  $COF = $row['CostaRica'];
  $PHF = $row['Philippines'];
  $CHF = $row['China'];
  $PUF = $row['PuertoRico'];
  $CUF = $row['Cuba'];
}

?>
each of them has a numerical value, and i want it to do something like the max() function, but i want it to return the name of the variable as well? Is that even possible?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

max($row);
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

How about adding

Code: Select all

ORDER BY DESC LIMIT 1
to the end of your query?
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

neophyte wrote: ORDER BY Country DESC LIMIT 1
Fixed! :wink:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

foobar wrote:
neophyte wrote: ORDER BY Country DESC LIMIT 1
Fixed! :wink:
Country is not the column that has the numerical values.. its the id :wink:
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

To make up for my obvious miss of your line post-snippet, try this:

Code: Select all

<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("countries", $conn);

$result = mysql_query("SELECT UnitedStates, GreatBritain, France, Spain, Nicaragua, Hawaii, Panama, Jamaica, Sudan, Libya, India, Algeria, Vietnam, CostaRica, Philippines, China, PuertoRico, Cuba FROM forts WHERE Country = '$ID'")
  or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{ 
  foreach($row as $key => $val) {
    if (isset($$key)) {
      $$key += $val;
    } else {
      $$key = $val;
    }
}

?>
You now have all the countries scores stored in a variable with their name (e.g. $UnitedStates)

Or instead of posting in seperate variables, use an array:

Code: Select all

<?php

$totals = array();

while ($row = mysql_fetch_assoc($result)) {
    foreach($row as $key => $val) {
        if (isset($totals[$key])) {
            $totals[$key] += $val;
        } else {
            $totals[$key] = $val;
        }
    }
}

$winner = max($totals);
$totals = array_flip($totals);

echo "The winner is {$totals[$winner]}!!";

?>
Post Reply