pb1uk Posted March 18, 2011 Report Share Posted March 18, 2011 Hi all, I know how to do this in normal php, but putting into oo php (which i'm new to) is proving to be difficult and i'm struggling to get my head around it. Basically i have a table called ground which very simply looks like this: id name rating 1 test1 5 2 test2 2 3 test3 4 etc etc. What i want to do is to work out the average of the rating column. At the moment on the page i have <?php $rating = new Ground(); $rating = $this->getRating(); ?> then in my ground class public function getRating() { $sql = "SELECT SUM(rating) AS average FROM ground"; $this->executeQuery($sql); $this->getRowCount(); // returns number of rows // here i need to work out the average! } I've tried a number of things in the class to try and work out the average, but for some reason i can't get the sum value. I admit the code above maybe and probably is riddled with errors, go easy on me am still new to the OO world! Any help would be greatly appreciated, Thanks, pb1uk Quote Link to comment Share on other sites More sharing options...
andrewgreenwood247@hotmail Posted March 18, 2011 Report Share Posted March 18, 2011 (edited) I'm having the same problem too. with returning values from a method in a class. Confusing isn't it! just thinking about it now, you could try using return($value); at the end of your function code. Once you have worked out the average value, set that to the $value variable and pass it through the return(); Can't try it out myself as I'm stuck at work for a few more hours, otherwise I would have tried it myself. Im a noob so can't guarantee this will work but just an idea. Edited March 18, 2011 by AndyG23 Quote Link to comment Share on other sites More sharing options...
pb1uk Posted March 18, 2011 Author Report Share Posted March 18, 2011 (edited) I have managed to find out how to do it. Although when i had done it what i needed to do was different, but this is a start! Cheers for the point in the right directions though Andy. I have changed the code was the call states the row of the db to look in $test_rating = new Ground(); $row = "test_rating_1"; echo $test_rating->getRating($row); Then in my ground class public function getRating($row) { $total=0; //intialise total variable // select all rows except those with zero $sql = "SELECT $row FROM ground WHERE $row != 0"; $this->executeQuery($sql); // run query $count = $this->getRowCount(); // get number of rows if($count==0){ // if now rows returned return the following message return "No one has rated this yet"; } else{ while ($this->moveNext()) { // get the total rating $value = $this->$row; $total = $total + $value; } // work out average return $total/$count . " from " . $count . " ratings"; } } Hope this helps. (moveNext, executeQuery and getRowCount are all functions from my database class, would have to be replaced with yours or mysql functions for this code to work) Edited March 18, 2011 by pb1uk Quote Link to comment Share on other sites More sharing options...
falkencreative Posted March 18, 2011 Report Share Posted March 18, 2011 One thing I thought I would point out, in case you didn't know this... There is a SQL function specifically for calculating averages. Using this might be more efficient? http://www.w3schools.com/sql/sql_func_avg.asp Quote Link to comment Share on other sites More sharing options...
andrewgreenwood247@hotmail Posted March 18, 2011 Report Share Posted March 18, 2011 (edited) Ha ha. I always do things the hard way dont know about you pb1, then someone will say to me "Andy, you could have done it like this!"... I am impressed with that bit of code you engineered though. Edited March 18, 2011 by AndyG23 Quote Link to comment Share on other sites More sharing options...
pb1uk Posted March 21, 2011 Author Report Share Posted March 21, 2011 Thanks hahaha. I did try he avg function Ben (thanks for the pointer though) but couldn't get it to work. I also tried sum to add up all the numbers as well and that didn't work. Was probably my object oriented syntax that messed it up. Unfortunately Andy that tends to happen to me a lot, i start thinking about how to code up a problem and end up over thinking and overcomplicating it, hopefully over time this will improve! Quote Link to comment Share on other sites More sharing options...
Guest webdev Posted April 2, 2011 Report Share Posted April 2, 2011 You can try it with PHP and Mysql AVG() function. Check the complete code. http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-avg%28%29.php Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.