webguync Posted March 26, 2010 Share Posted March 26, 2010 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--> Quote Link to comment https://forums.phpfreaks.com/topic/196641-help-with-creating-average-function/ Share on other sites More sharing options...
mikesta707 Posted March 26, 2010 Share Posted March 26, 2010 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) Quote Link to comment https://forums.phpfreaks.com/topic/196641-help-with-creating-average-function/#findComment-1032411 Share on other sites More sharing options...
MatthewJ Posted March 26, 2010 Share Posted March 26, 2010 Or, couldn't you just do it on the way out of the db with SELECT AVG(score) FROM table ? Well, I guess since the columns are all separate and not all stored in score that wouldn't work Quote Link to comment https://forums.phpfreaks.com/topic/196641-help-with-creating-average-function/#findComment-1032422 Share on other sites More sharing options...
webguync Posted March 26, 2010 Author Share Posted March 26, 2010 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/196641-help-with-creating-average-function/#findComment-1032431 Share on other sites More sharing options...
mikesta707 Posted March 26, 2010 Share Posted March 26, 2010 well where does $row come from? can i see the whole script? if you are doing a query then Matthew's advice would work also Quote Link to comment https://forums.phpfreaks.com/topic/196641-help-with-creating-average-function/#findComment-1032432 Share on other sites More sharing options...
webguync Posted March 26, 2010 Author Share Posted March 26, 2010 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/196641-help-with-creating-average-function/#findComment-1032441 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.