I have an array which each one of them have a different sum on database (maybe some of them are empty or there is no record for them)
i need to know which one has bigger sum THAN ALL THE OTHERS (ON MY ARRAY) and put that one in $bigger variable, ($bigger = $value)
My try:
$new = 0;
$myarray = array('2', '5', '17', '55');
foreach($myarray as $value) {
$before = intval($new);
$query = mysql_query("SELECT SUM(total) AS totals FROM table WHERE id='$value'");
$result = mysql_fetch_assoc($query);
$new = intval($result['totals']);
if ($new > $before){
$biggest=intval($value);
}
}
but it only shows if the newer one has bigger value than the older one, not bigger than all of them
Any help is appreciated
I would eliminate the looping altogether and do it like this:
Note that is you want to receive data for all id’s in sorted order you can simply remove the
LIMITclause, and add a looping to read out each row of the result set.I would also recommend not using the
mysql_*functions as they are deprecated. If you are just learning PHP, you should learn usingmysqli_*functions, PDO or something that will enjoy support in the future.You should also learn how to test for failure conditions on your database queries. My example is a simple one, but production code should test the success/failure of the query and of the fetch operation.