troubleshooting Question

Accessing Joomla DB and retrieving AVG of value column

Avatar of Jesse Matlock
Jesse MatlockFlag for United States of America asked on
PHPMySQL ServerJoomla
22 Comments1 Solution701 ViewsLast Modified:
Hello,
I'm trying to retrieve the average value for ratings within a Joomla DB table. This is not the standard Joomla article rating, but a rating extension for a component (YooTheme Zoo).

Using Joomla's database object I need to retrieve the average rating value, for all records within a table

I have attached the code that I've used so far, without success.
I have also attached a screenshot of the DB table so you can see the structure.

If possible it would be great to see the SQL Query as well as the code used echo/print the result to a page.

Thanks!

function avgRating() {
	$db   =& JFactory::getDBO();

	  $sql = "SELECT AVG(value) FROM #__zoo_core_rating WHERE item_id = 4";
	  $db->setQuery( $sql );
	  if (!$result = $db->query()) {
	    echo $db->stderr();
	    return false;
	  }

	  return $db->getNumRows( $result );

	}

	echo 'The average rating is ' . avgRating();
snapshot--17.png
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 22 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 22 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros