Reputation: 31
I really need some help with some simple math in PHP. I've never done this kind of thing before but find myself in need of it. Here is what I am trying to accomplish;
I have a MySQL table called 'eval' which contains test scores and comments for all the students in my class.
Every month I test and evaluate my students in 4 areas, each having max total of 10 points.
The table rows look like this;
ID name participation speaking listening writing reading comment
1 John Smith Excellent 7 7 7 7 Teacher Comment.
2 Jane Brown Excellent 6 7 7 6 Teacher Comment.
3 Mike Jones Very Good 6 7 7 6 Teacher Comment.
I then display this data on a page in a table.
What I want to do is create an additional column in the table on the page and use some PHP to add up the 4 values (speaking, listening, writing, reading) and display the result as a percentage grade in the extra column.
Here is the code for selecting the table in MySQL
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$colname_rsEval = "-1";
if (isset($_GET['group'])) {
$colname_rsEval = $_GET['group'];
}
mysql_select_db($database_sgnesldb, $sgnesldb);
$query_rsEval = sprintf("SELECT * FROM eval WHERE `group` = %s", GetSQLValueString($colname_rsEval, "text"));
$rsEval = mysql_query($query_rsEval, $sgnesldb) or die(mysql_error());
$row_rsEval = mysql_fetch_assoc($rsEval);
$totalRows_rsEval = mysql_num_rows($rsEval);
?>
And here is the code for the layout on the page
<td width="12" align="center" valign="top"><?php echo $row_rsEval['ID']; ?></td>
<td width="145" valign="top" class="darkbrownbold"><?php echo $row_rsEval['name']; ?></td>
<td width="72" align="center" valign="top"><?php echo $row_rsEval['participation']; ?></td>
<td width="55" align="center" valign="top" class="darkbrownbold"><?php echo $row_rsEval['speaking']; ?></td>
<td width="53" align="center" valign="top" class="darkbrownbold"><?php echo $row_rsEval['listening']; ?></td>
<td width="42" align="center" valign="top" class="darkbrownbold"><?php echo $row_rsEval['writing']; ?></td>
<td width="44" align="center" valign="top" class="darkbrownbold"><?php echo $row_rsEval['reading']; ?></td>
<td width="25" align="center" valign="top"> </td>
<td width="350" valign="top"><?php echo nl2br($row_rsEval['comment']); ?></td>
<td width="45" align="center" valign="top" class="darkbrownbold"><?php echo $row_rsEval['top5']; ?></td>
I need the percentage total to display in the column before 'Comments'
Thanks everyone.
Upvotes: 2
Views: 151
Reputation: 88647
(Assuming you want to make this calculation when the rows are inserted)
function get_percentage_grade ($speaking, $listening, $writing, $reading, $possibleTotal = 40) {
return round((($speaking + $listening + $writing + $reading) / $possibleTotal) * 100);
}
$name = 'Dave Random';
$participation = 'Excellent';
$speaking = 7;
$listening = 7;
$writing = 7;
$reading = 7;
$comment = 'Teacher Comment.';
$percentage = get_percentage_grade($speaking,$listening,$writing,$reading);
$query = "
INSERT INTO eval
(name, participation, speaking, listening, writing, reading, percentage, comment)
VALUES
('$name', '$participation', '$speaking', '$listening', '$writing', '$reading', '$percentage', '$comment')
";
The function get_percentage_grade()
is the bit you want. This takes 5 arguments - speaking, listening, writing, reading (as numbers, int or float) and an optional "possible total" argument, that represents the maximun possible score. This, as you can see, defaults to 40 - you can alter this default simply by altering the function definition. It returns the percentage score, rounded to the nearest integer.
And don't forget to escape your values in your query :-)
Upvotes: 0
Reputation: 13166
I hope I could get your mean. You have 4 item and each one can be at most 10. So your maximum value is 40. Now you need to do something like this :
<?php
$max = 40;
$current = $speaking + $listening + $writing + $reading;
$percentage = (100 * $current) / 40;
echo(number_format($percentage,'2','.','')); // echo with 2 digits after the point.
?>
Your needed value is $percentage
;
Upvotes: 0
Reputation: 212412
Use this as your SQL query
SELECT id,
name,
participation,
speaking,
listening,
writing,
reading,
(speaking + listening + writing + reading) / 40 * 100 AS percentagetotal,
comment
FROM eval
Upvotes: 6
Reputation: 4165
Assuming you have stored the scores in different variables, you can do something like this:
$total = $speaking + $listening + $writing + $reading;
$maxTotal = 40;
$percentage = round($total*100.0/$maxTotal);
And then display the $percentage value for each student.
Upvotes: 0
Reputation: 4397
First add a column total_percentage
You can do that directly in MySQL using the following the query:
UPDATE `eval` SET `percentage`=((`speaking`+ `listening` + `writing` + `reading`)/0.4)
Upvotes: 0