Skip to main content
May 30, 2006
Answered

PHP Code to perform calculations

  • May 30, 2006
  • 8 replies
  • 690 views
How can one use php code to calculate an everage of all the entries in a table column? I am sure this is easy but I have no clue. I found this code on the net. I understand it but I am not sure how i reference my database since it is referred to as a require one line. so basically I am not sure what comes after the "From" in the code below.

<?php
function average_value()
{
$query = "SELECT SUM(helpful) FROM evaluation";
$result = mysql_query($query);

// Number of people in the table
$num = mysql_num_rows($result);

$sum = mysql_result($result, 0);
$average = ($sum / $num);
$average = round($average, 1);

return 'The average score for Helpful is '.$average;
}
?>

Darren
This topic has been closed for replies.
Correct answer
Hung Kuen Kung Fu wrote:
> I still get nothing returned on the screen. I assume this is because I have no php code to display the result.
> I am not sure how to get this to happen. Thanks.

Just create a recordset in the normal way, but using the SQL that I gave
you. You can then drag the values into your page from the Bindings panel.

--
David Powers
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "Foundation PHP 5 for Flash" (friends of ED)
http://foundationphp.com/

Hello David,

Thanks so much for the help. It was as easy as you said. I guess I wasn't seeing the forest with all the trees in the way.

I created the new recordset through the server bahaviour panel. Then I manually swaped the select statement with the one you suggested. I then had to manually create the echo statements for the results since it no longer appeared in the bindings panel once I edited the select statement.

This is a great help to me in developing evaluation and survey content for the students at our school. It is so simple in retrospect but so powerfully useful.

Thanks again David for your patience and guidance.

Darren

8 replies

Inspiring
May 30, 2006
Hung Kuen Kung Fu wrote:
> I get the follwing error on the page: PHP Parse error:

Your parse error is almost certainly caused by this line:

> $query = "SELECT SUM($row_rscomments['helpful']) FROM $rscomments";

You can't use quotes around an associative array key in a double quoted
string. Your line should be either this:

$query = "SELECT SUM($row_rscomments[helpful]) FROM $rscomments";

or this enclosing the associative array in curly braces):

$query = "SELECT SUM({$row_rscomments['helpful']}) FROM $rscomments";

However, you're actually making too much work for yourself (and PHP).
The answer is to use the MySQL AVG() function:

SELECT AVG(thisColumn) AS thisColumn
FROM thisTable

--
David Powers
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "Foundation PHP 5 for Flash" (friends of ED)
http://foundationphp.com/
May 31, 2006
OK,

I am trying again with your suggestion but I don't know what to add to display my result: So if you could let me know what I need and check to see if what I have so far makes sense. I am basically using the php generated from my recodset to aid me.

<?php
mysql_select_db($database_evaluate, $evaluate);
$query_rsscore = "SELECT * FROM rating";
$rsscore = mysql_query($query_rsscore, $evaluate) or die(mysql_error());
$row_rsscore = mysql_fetch_assoc($rsscore);
$totalRows_rsscore = mysql_num_rows($rsscore);

$query="Select AVG(look), AVG(overall) FROM rating";
$result=mysql_query($query);

/* This is where I don't know what to do to get the result displayed */

?>


Thanks

****Actually this is the whole thing...if that helps*****
May 30, 2006
OK

When I try to use this code:
I get the follwing error on the page: PHP Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in c:\Inetpub\wwwroot\rate\administration.php on line 170

Thanks in advance