This is the community forum. For a developer response use the Client Area.
Follow us on Facebook, Twitter and YouTube!

get the star rating from comments page
#1

On my site each product page has a comment form and my goal here is to get the average rating and put it on the product listing page,
here's what I have but I get an invalid argument error:

PHP Code:
<?php 
function get_product_rating() {

foreach (
$products as $product) {

function
cmtx_average_rating() { //get average rating

global $cmtx_mysql_table_prefix, $cmtx_page_id; //globalise variables

$result = mysql_query("SELECT AVG(rating) FROM `" . $cmtx_mysql_table_prefix . "comments` WHERE `is_approved` = '1' AND `rating` != '0' AND `page_id` = '$cmtx_page_id'");

$average = mysql_fetch_assoc($result);

$average = round($average["AVG(rating)"] / 0.5) * 0.5;

return
$average;

}

}

}
Reply
#2

I'm not sure where to start here. Can you explain more about your objective, because it doesn't make sense to me how you're approaching it with the code. I assume you have a page with a list of products, and if I were to visit each product I would see its average rating, and what you want to do is display each of those average ratings on the product listing page. So on the product listing page, you have product 1 and its average rating, then product 2 and its average rating, and so on. Is that correct?

I can't see where you are actually calling the cmtx_average_rating() function. It looks like you're just re-creating the same function lots of times. What is the value of the variable $product? How are you supplying the value for the $cmtx_page_id variable? What's the full error message that you're getting?

Have you completed the interview?
Reply
#3

Quote:I assume you have a page with a list of products, and if I were to visit each product I would see its average rating, and what you want to do is display each of those average ratings on the product listing page. So on the product listing page, you have product 1 and its average rating, then product 2 and its average rating, and so on. Is that correct?

Yep that is what I want to do.

Quote:I can't see where you are actually calling the cmtx_average_rating() function. It looks like you're just re-creating the same function lots of times. What is the value of the variable $product? How are you supplying the value for the $cmtx_page_id variable? What's the full error message that you're getting?
Not sure of the value of $product, how would I find that?

this is my function for commentics:

PHP Code:
<?php 
function comments($key) {

$cmtx_page_id = "cmtx_reference";
$cmtx_reference = $key;
$cmtx_path = "././comments/";
define('IN_COMMENTICS', 'true'); //no need to edit this line
require $cmtx_path . "includes/commentics.php"; //no need to edit this line

}

this is how it's called:

Code:
<div tal:content="php:comments(product['mykey'])" class="review_container"></div>

Sorry but am a little lost, so I need to call the cmtx_average_rating() variable for each product key?
Reply
#4

PHP Code:
<?php
function cmtx_average_rating ($cmtx_page_id) { //get average rating

global $cmtx_mysql_table_prefix; //globalise variables

$result = mysql_query("SELECT AVG(rating) FROM `" . $cmtx_mysql_table_prefix . "comments` WHERE `is_approved` = '1' AND `rating` != '0' AND `page_id` = '$cmtx_page_id'");

$average = mysql_fetch_assoc($result);

$average = round($average["AVG(rating)"] / 0.5) * 0.5;

return
$average;

}


/* Product Listing */
foreach ($products as $product) { //loop through each product

//Average Rating
echo cmtx_average_rating($cmtx_page_id); // (change the $cmtx_page_id variable here, according to which product is in the loop)

}
?>

Have you completed the interview?
Reply
#5

(12-Oct-2012, 09:31 PM)Steven Wrote:  
PHP Code:
<?php
function cmtx_average_rating ($cmtx_page_id) { //get average rating

global $cmtx_mysql_table_prefix; //globalise variables

$result = mysql_query("SELECT AVG(rating) FROM `" . $cmtx_mysql_table_prefix . "comments` WHERE `is_approved` = '1' AND `rating` != '0' AND `page_id` = '$cmtx_page_id'");

$average = mysql_fetch_assoc($result);

$average = round($average["AVG(rating)"] / 0.5) * 0.5;

return
$average;

}


/* Product Listing */
foreach ($products as $product) { //loop through each product

//Average Rating
echo cmtx_average_rating($cmtx_page_id); // (change the $cmtx_page_id variable here, according to which product is in the loop)

}
?>

I am sorry, I am stupid, but it doesn't work..What i really must do? I must change $cmtx_page_id to $cmtx_page_1 (for example)? and which line i must change pls?
Reply
#6

Hi,

You didn't give any details of what you're trying to do, or how it's not working. I'm going to assume that you're not going through a loop like the original poster was doing. If so, try the following. I've added comments showing the things you need to change.

PHP Code:
<?php
function cmtx_average_rating($cmtx_page_id) {

global
$cmtx_mysql_table_prefix;

$result = mysql_query("SELECT AVG(rating) FROM `" . $cmtx_mysql_table_prefix . "comments` WHERE `is_approved` = '1' AND `rating` != '0' AND `page_id` = '$cmtx_page_id'");

$average = mysql_fetch_assoc($result);

$average = round($average["AVG(rating)"] / 0.5) * 0.5;

return
$average;

}
?>

<?php
$cmtx_page_id
= 'test'; //change this

define('IN_COMMENTICS', '1');
require
'comments/includes/db/connect.php'; //you *may* need to change this

$cmtx_page_id = htmlspecialchars($cmtx_page_id, ENT_QUOTES, 'UTF-8');
$cmtx_page_id = mysql_real_escape_string($cmtx_page_id);

$query = mysql_query("SELECT `id` FROM `" . $cmtx_mysql_table_prefix . "pages` WHERE `page_id` = '$cmtx_page_id'");
$result = mysql_fetch_assoc($query);
$id = $result['id'];

echo
cmtx_average_rating($id);
?>

Have you completed the interview?
Reply
#7

You explained it very exactly and everything is working now. Thank you very much for your help. AZJOL
Reply
#8

(12-Oct-2012, 09:31 PM)Steven Wrote:  
PHP Code:
<?php
function cmtx_average_rating ($cmtx_page_id) { //get average rating

global $cmtx_mysql_table_prefix; //globalise variables

$result = mysql_query("SELECT AVG(rating) FROM `" . $cmtx_mysql_table_prefix . "comments` WHERE `is_approved` = '1' AND `rating` != '0' AND `page_id` = '$cmtx_page_id'");

$average = mysql_fetch_assoc($result);

$average = round($average["AVG(rating)"] / 0.5) * 0.5;

return
$average;

}


/* Product Listing */
foreach ($products as $product) { //loop through each product

//Average Rating
echo cmtx_average_rating($cmtx_page_id); // (change the $cmtx_page_id variable here, according to which product is in the loop)

}
?>

Didn't get around to testing this, when I do I'll post back the result, I'm sure others will find this useful too.
Reply


Possibly Related Threads…
Thread / Author Replies Views Last Post
Last Post by Tim
06-Jan-2021, 08:56 PM
Last Post by Tim
18-Oct-2019, 11:45 AM
Last Post by Gevill
23-Jul-2019, 01:17 AM
Last Post by Seven Thunders
21-Aug-2018, 04:05 AM
Last Post by Steven
16-Feb-2018, 09:15 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)