My table looks like this. This is my code to display the average per category. But I want to find the highest value per column. Like in C1.1 I want find the highest average value in that column.
<div class="container" class="col-lg-5">
<table class="table table-striped table table-bordered" >
<tr><th>Campus</th>
<th>No. of Staff</th>
<th>C1.1</th>
<th>C1.2</th>
<th>C1.3</th>
<th>C1.4</th>
<th>C1.5</th>
<th>C1.6</th>
<th>C2.1</th>
<th>C2.2</th>
<th>C2.3</th>
<th>C3.1</th>
<th>C3.2</th>
<th>C3.3</th>
<th>C3.4</th>
<tr>
<td>MAIN 1 - CABEIHM</td>
<td>
<?php
$queryn ="SELECT COUNT(dept_code) FROM employment where dept_code=3 and empg_code=1";
$resultn = mysql_query($queryn) or die(mysql_error());
while($row1 = mysql_fetch_array($resultn)){
echo "".$row1['COUNT(dept_code)'];
echo "<br />";
}
?>
</td>
<td><!--1.1-->
<?php
$query1 = ("SELECT ROUND(AVG(Compv11),2) , dept_code , camp_code
FROM performance
INNER JOIN employment
ON employment.emp_code=performance.emp_id AND employment.dept_code=performance.dept_id
WHERE empg_code=1 AND dept_id=3
");
$result1 = mysql_query($query1) or die(mysql_error());
[enter image description here][1]
// Print out result
while($row = mysql_fetch_array($result1))
{
echo "".$row['ROUND(AVG(Compv11),2)'];
}
?>
To get the row with the highest average value from the C1.1 column, your best option is to calculate this in PHP.
You are currently outputting the values directly as soon as you get them from the database. You'll need to store the average values in a variable, so you can get the highest value later:
$averages = array();
$query1 = ("SELECT ROUND(AVG(Compv11),2) , dept_code , camp_code
FROM performance
INNER JOIN employment
ON employment.emp_code=performance.emp_id AND employment.dept_code=performance.dept_id
WHERE empg_code=1 AND dept_id=3
");
$result1 = mysql_query($query1) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result1))
{
$averages[1] = $row['ROUND(AVG(Compv11),2)'];
echo "".$row['ROUND(AVG(Compv11),2)'];
}
Note that I'm adding the result to index 1
of $averages
, because your variables are called $query1
and $result1
and I assume that for all your other rows you use incrementing variables (ie $query2
and $result2
for the second row, etc.). What follows is not very efficient, I'll give you an alternative afterwards.
Once you have all the average values in $averages
, you can sort the array:
arsort($averages);
This will sort the array in reverse order (meaning the first entry will be the one with the highest value) while keeping the index to value association. Now we reset()
the array to be safe, after which you can get the index using the key()
function and the value itself using the current()
function:
reset($averages);
$highest_index = key($averages);
$highest_value = current($averages);
$highest_query = "query" . $highest_index;
$highest_result = "result" . $highest_index;
Now you know that the highest value came from $$highest_query
and its result is in $$highest_result
, in case you want to do something with that information. You can, for example, use mysql_data_seek($$highest_result, 0)
to reset it and then you can fetch its row again using mysql_fetch_array($$highest_result)
.
Although you should really consider moving away from the old, unsupported, mysql_*
functions and toward mysqli
or PDO
for your database communication.
Now for that alternative I mentioned:
Depending on what you want to do with the highest value (you say you want to output it and "make a report on it"), you probably also want to know which empg_code
and dept_id
you're filtering on in your query, or the dept_code
or camp_code
you're selecting along with the average value.
So let's fill our $averages
array with more than just the average values:
while($row = mysql_fetch_array($result1))
{
$averages[] = array(
'avg' => $row['ROUND(AVG(Compv11),2)'],
'empg_code' => 1,
'dept_id' => 3,
'dept_code' => $row['dept_code'],
'camp_code' => $row['camp_code']
);
echo "".$row['ROUND(AVG(Compv11),2)'];
}
Now we can use usort
to sort the array using a custom function:
usort($averages, function($a, $b) {
if ($a['avg'] == $b['avg']) {
return 0;
}
// note: reverse check of the example, because we want to
// sort in DESCENDING order
// https://php.net/usort
return ($a['avg'] > $b['avg']) ? -1 : 1;
});
Now the data for the entry with the highest value will be the first entry of the $averages
array:
$highest = $averages[0];
echo $highest['avg']; // highest average value
echo $highest['empg_code']; // 1
echo $highest['dept_id']; // 3
echo $highest['dept_code']; // dept_code for the highest average value
echo $highest['camp_code']; // camp_code for the highest average value
Hope that helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.