Jump to content
Killersites Forums

OO PHP sql working out average problem


Recommended Posts

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


$rating =  new Ground();
$rating = $this->getRating();


then in my ground class


public function getRating()
  $sql = "SELECT SUM(rating) AS average FROM ground";
  $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,





Link to comment
Share on other sites

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 by AndyG23
Link to comment
Share on other sites

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 now rows returned return the following message
		return "No one has rated this yet";
		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 by pb1uk
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

  • 2 weeks later...

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...