Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

PHP Code to perform calculations

Guest
May 30, 2006 May 30, 2006
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
TOPICS
Server side applications
689
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Deleted User
May 31, 2006 May 31, 2006
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 f...
Translate
Guest
May 30, 2006 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 30, 2006 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/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 30, 2006 May 30, 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*****
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 31, 2006 May 31, 2006
Hung Kuen Kung Fu wrote:
> $query="Select AVG(look), AVG(overall) FROM rating";

Change this line to this:

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

--
David Powers
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "Foundation PHP 5 for Flash" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 31, 2006 May 31, 2006
Thanks for the help....

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.

Darren
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 31, 2006 May 31, 2006
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/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 31, 2006 May 31, 2006
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 31, 2006 May 31, 2006
LATEST
Hung Kuen Kung Fu wrote:
> Thanks so much for the help. It was as easy as you said.

Glad to have been of help.

--
David Powers
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "Foundation PHP 5 for Flash" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines