Jump to content

help with creating average function


webguync

Recommended Posts

what would be the best way to average 8 rows of numbers and create an average rounded to one decimal so 4.75 would be 4.8, 4.67 would be 4.6 etc.

 

I would like to create an average function and echo out the result. The data is coming from a MySQL database like this

 

  echo "<td>" . $row['score1'] . "</td>";
  echo "<td>" . $row['score2'] . "</td>";
  echo "<td>" . $row['score3'] . "</td>";
  echo "<td>" . $row['score4'] . "</td>";
  echo "<td>" . $row['score5'] . "</td>";
  echo "<td>" . $row['score6'] . "</td>";
  echo "<td>" . $row['score7'] . "</td>";
  echo "<td>" . $row['score8'] . "</td>";
echo "<td>" . $row['average'] . "</td>";<!--would like an average of scores 1-8-->

Link to comment
https://forums.phpfreaks.com/topic/196641-help-with-creating-average-function/
Share on other sites

if the keys are always called 'scorex' where x is some number between 1 and 8

//take sum
$sum = 0;
for ($i = 1; $i <= 8; $i++){
$sum += $row['score'.$i];
}
$row['average'] = $sum/8;

//round $average
$row['average'] = round($row['average'], 1)

that might work mikesta707, but I get undefined variable notices for row and undefined index for average.

I have

//take sum

$sum = 0;
for ($i = 1; $i <= 8; $i++){
$sum += $row['score'.$i];
}
$row['average'] = $sum/8;

//round $average
$row['average'] = round($row['average'], 1);

 

and...

echo "<td>" . $row['average'] . "</td>";

sure, here is the whole script

 

<?php
ini_set("display_errors","1");
ERROR_REPORTING(E_ALL);
$con = mysql_connect("localhost","username","pw");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("DB_Name", $con);


$result = mysql_query("SELECT rpc.employee_id,rpc_.employee_name, roster.title,.assessor_id,.assessor_name,rpc.score1,rpc.score2,rpc.score3,rpc.score4,rpc.score5,rpc.score6,rpc.score7,rpc.score8,rpc.date_created,rpc.date_uploaded FROM rpc LEFT JOIN  roster USING (employee_id) ORDER BY rpc.employee_id, rpc.date_uploaded ASC") or die(mysql_error());
echo "<table>
<tr>

<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Title</th>
<th>score 1</th>
<th>score 2</th>
<th>score 3</th>
<th>score 4</th>
<th>score 5</th>
<th>score 6</th>
<th>score 7</th>
<th>score 8</th>
<th>Average Score (1-</th>
<th>Assessor Name</th>
<th>Assessor ID</th>
<th>Call Number (1-4)</th>
<th>Date Created</th>
<th>Date Uploaded</th>
</tr>";
//take sum

$sum = 0;
for ($i = 1; $i <= 8; $i++){
$sum += $row['score'.$i];
}
$row['average'] = $sum/8;

//round $average
$row['average'] = round($row['average'], 1);

//count Call Numbers and list by date entered
$count = 0;
$CallNumber = 1;

while($row = mysql_fetch_array($result))
  {

$currentID = $row['employee_id'];

    if($count == 0 || $currentID != $previousID) {
        $CallNumber = 1; }
    else {
        $CallNumber++;
	}

  echo "<tr>";

  echo "<td>" . $row['employee_id'] . "</td>";
  echo "<td>" . ucwords($row['employee_name']) . "</td>";

  echo "<td>" . $row['title'] . "</td>";
  echo "<td>" . $row['score1'] . "</td>";
  echo "<td>" . $row['score2'] . "</td>";
  echo "<td>" . $row['score3'] . "</td>";
  echo "<td>" . $row['score4'] . "</td>";
  echo "<td>" . $row['score5'] . "</td>";
  echo "<td>" . $row['score6'] . "</td>";
  echo "<td>" . $row['score7'] . "</td>";
  echo "<td>" . $row['score8'] . "</td>";

  echo "<td>" . $row['average'] . "</td>";<!--average score will go here-->
  echo "<td>" . $row['assessor_name'] . "</td>";
  echo "<td>" . $row['assessor_id'] . "</td>";
  echo "<td>" . $CallNumber . "</td>";
  echo "<td>" . $row['date_created'] . "</td>";
  echo "<td>" . $row['date_uploaded'] . "</td>";
  echo "</tr>";

$previousID = $currentID;
    
    $count++;

  }
echo "</table>";
mysql_close($con);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.