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

Average Rating
#1

No matter how many combinations I try I cannot seem to get an accurate "average" rating for my reviews. The below code "should" produce 4.3 stars (through demo info put into database). Instead it shows 5 stars. Can you help me find what is wrong? Also how to show 4.3 out of 5 stars instead of 4 out of 5 stars?

<?php $cmtx_identifier = 'Location';
$cmtx_path = '';
require_once $cmtx_path . 'includes/db/connect.php';
$cmtx_identifier = mysqli_real_escape_string($cmtx_link, $cmtx_identifier);
$page_query = mysqli_query($cmtx_link, "SELECT `id` FROM `" . $cmtx_mysql_table_prefix . "pages` WHERE `identifier` = '" . $cmtx_identifier . "'");
$page = mysqli_fetch_assoc($page_query);
$id = $page['id'];
$result = mysqli_query($cmtx_link, "SELECT AVG(`rating`)
    FROM (
    SELECT `rating` FROM `" . $cmtx_mysql_table_prefix . "comments` WHERE `is_approved` = '1' AND `rating` != '0' AND `page_id` = '$id'
    UNION ALL
    SELECT `rating` FROM `" . $cmtx_mysql_table_prefix . "ratings` WHERE `page_id` = '$id'
    )
    AS `average`
    ");
$votes = mysqli_num_rows($result);
$average = round($result_avg["AVG(rating)"]/0.5)*0.5;
$max_rating = round($result_avg["MAX(rating)"]/0.5)*0.5;
$min_rating = round($result_avg["MIN(rating)"]/0.5)*0.5;
$count_rating = $result_avg["count(*)"];

if ($average == 1) {
    echo '<br><img src="' . $cmtx_path . 'https://www.****/images/stars/star_full.png"><br><span itemprop="average">1</span> out of <span itemprop="best">5</span></span>';
} elseif ($average == 2) {
    echo '<br><img src="' . $cmtx_path . 'https://www.****/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****/images/stars/star_full.png"><br><span itemprop="average">2</span> out of <span itemprop="best">5</span></span>';
}  elseif ($average == 3) {
    echo '<br><img src="' . $cmtx_path . 'https://www.****/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****/images/stars/star_full.png"><br><span itemprop="average">3</span> out of <span itemprop="best">5</span></span>';
}  elseif ($average == 4) {
    echo '<br><img src="' . $cmtx_path . 'https://www.****/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****/images/stars/star_full.png"><br><span itemprop="average">4</span> out of <span itemprop="best">5</span></span>';
}  else {
    echo '<br><img src="' . $cmtx_path . 'https://www.****/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****/images/stars/star_full.png"><br><span itemprop="average"><font style="font-size:1em; font-family: arial; color:#000000;">5</font></span> out of <span itemprop="best"><font style="font-size:1em; font-family: arial; color:#000000;">5 Stars</font></span></span>';
}
?>
Reply
#2

I think the first issue is that $result_avg isn't defined. Try changing all four instances of it to $result.

Only full stars are supported. If you wanted you could put some half stars into the images folder, round to the nearest half and change the if statements accordingly.

Have you completed the interview?
Reply
#3

Thank you kindly.

I have the correct "stars" showing (whole stars as I have not got a clue about half stars, lol) now I just need to get "4 out of 5 stars" to show as "4.3 out of 5 stars" in the "text" area below the stars.
Like this:

****
4.3 out of 5 Stars

This is the previous version of Commentics (as I assume you can tell by the code) and it is HIGHLY modified or I would upgrade. The newer version looks awesome by the way!

Here is what I have:

<div itemscope itemtype="https://data-vocabulary.org/Review-aggregate">
<span itemprop="itemreviewed"><p align="right" style="line-height: 1.25em; padding-top:4px;"><font style='font-size:1.15em; font-family: arial; color:#000000;'>****</font><font style='font-size:1.15em; font-family: arial; color:#000000;'><br>****</span>
<span itemprop="rating" itemscope itemtype="https://data-vocabulary.org/Rating">

<?php $cmtx_identifier = 'Location';
$cmtx_path = '';
require_once $cmtx_path . 'includes/db/connect.php';
$cmtx_identifier = mysqli_real_escape_string($cmtx_link, $cmtx_identifier);
$page_query = mysqli_query($cmtx_link, "SELECT `id` FROM `" . $cmtx_mysql_table_prefix . "pages` WHERE `identifier` = 'Location'");
$page = mysqli_fetch_assoc($page_query);
$id = $page['id'];
$result = mysqli_query($cmtx_link, "SELECT AVG(`rating`)
FROM (
SELECT `rating` FROM `" . $cmtx_mysql_table_prefix . "comments` WHERE `is_approved` = '1' AND `rating` != '0' AND `page_id` = '10001' AND reply_to = '0'
UNION ALL
SELECT `rating` FROM `" . $cmtx_mysql_table_prefix . "ratings` WHERE `page_id` = '10001'
)
AS `average`
");
$votes = mysqli_num_rows($result);
$average = mysqli_fetch_assoc($result);
$average = $average["AVG(`rating`)"];
$average = round($average, 0);
if ($average == 1) {
echo '<br><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><span itemprop="average"><font face="arial" color="#000000" style="font-size: 0.95em">1</font></span> out of <span itemprop="best"><font face="arial" color="#000000" style="font-size: 0.95em">5</font></span></span>';
} elseif ($average == 2) {
echo '<br><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><br><span itemprop="average"><font face="arial" color="#000000" style="font-size: 0.95em">2</font></span> <font face="arial" color="#000000" style="font-size: 0.95em">out of</font> <span itemprop="best"><font face="arial" color="#000000" style="font-size: 0.95em">5</font></span></span>';
} elseif ($average == 3) {
echo '<br><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><br><span itemprop="average"><font face="arial" color="#000000" style="font-size: 0.95em">3</font></span> <font face="arial" color="#000000" style="font-size: 0.95em">out of</font> <span itemprop="best"><font face="arial" color="#000000" style="font-size: 0.95em">5</font></span></span>';
} elseif ($average == 4) {
echo '<br><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><br><span itemprop="average"><font face="arial" color="#000000" style="font-size: 0.95em">4</font></span> <font face="arial" color="#000000" style="font-size: 0.95em">out of</font> <span itemprop="best"><font face="arial" color="#000000" style="font-size: 0.95em">5</font></span></span>';
} else {
echo '<br><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><br><span itemprop="average"><font face="arial" color="#000000" style="font-size: 0.95em">5</font></span> <font face="arial" color="#000000" style="font-size: 0.95em">out of</font> <span itemprop="best"><font face="arial" color="#000000" style="font-size: 0.95em">5 Stars</font></span></span>';
}
?>
Reply
#4

Sorry I see what you mean now.

So above this line:
$average = round($average, 0);

You could put this:
$average_not_rounded = $average;

Then further down, just before it says "out of", for each one change the number that comes before it (1,2,3,4,5 etc) with <?php echo $average_not_rounded; ?>

Have you completed the interview?
Reply
#5

Thank you for your efforts Steven. However, it now shows: "out of 5" Just a blank space. I wonder if it has something to do with this: elseif ($average == 4)
Also, where is "$average_not_rounded" defined? Seems like it's just repeating $average ???

Here's what I have after your changes:

<?php $cmtx_identifier = 'Location';
$cmtx_path = '';
require_once $cmtx_path . 'includes/db/connect.php';
$cmtx_identifier = mysqli_real_escape_string($cmtx_link, $cmtx_identifier);
$page_query = mysqli_query($cmtx_link, "SELECT `id` FROM `" . $cmtx_mysql_table_prefix . "pages` WHERE `identifier` = 'Location'");
$page = mysqli_fetch_assoc($page_query);
$id = $page['id'];
$result = mysqli_query($cmtx_link, "SELECT AVG(`rating`)
FROM (
SELECT `rating` FROM `" . $cmtx_mysql_table_prefix . "comments` WHERE `is_approved` = '1' AND `rating` != '0' AND `page_id` = '10001' AND reply_to = '0'
UNION ALL
SELECT `rating` FROM `" . $cmtx_mysql_table_prefix . "ratings` WHERE `page_id` = '10001'
)
AS `average`
");
$votes = mysqli_num_rows($result);
$average = mysqli_fetch_assoc($result);
$average = $average["AVG(`rating`)"];
$average_not_rounded = $average;
$average = round($average, 0);
if ($average == 1) {
echo '<br><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><span itemprop="average"><font face="arial" color="#000000" style="font-size: 0.95em"><?php echo $average_not_rounded; ?></font></span> out of <span itemprop="best"><font face="arial" color="#000000" style="font-size: 0.95em">5</font></span></span>';
} elseif ($average == 2) {
echo '<br><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><br><span itemprop="average"><font face="arial" color="#000000" style="font-size: 0.95em"><?php echo $average_not_rounded; ?></font></span> <font face="arial" color="#000000" style="font-size: 0.95em">out of</font> <span itemprop="best"><font face="arial" color="#000000" style="font-size: 0.95em">5</font></span></span>';
} elseif ($average == 3) {
echo '<br><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><br><span itemprop="average"><font face="arial" color="#000000" style="font-size: 0.95em"><?php echo $average_not_rounded; ?></font></span> <font face="arial" color="#000000" style="font-size: 0.95em">out of</font> <span itemprop="best"><font face="arial" color="#000000" style="font-size: 0.95em">5</font></span></span>';
} elseif ($average == 4) {
echo '<br><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><br><span itemprop="average"><font face="arial" color="#000000" style="font-size: 0.95em"><?php echo $average_not_rounded; ?></font></span> <font face="arial" color="#000000" style="font-size: 0.95em">out of</font> <span itemprop="best"><font face="arial" color="#000000" style="font-size: 0.95em">5</font></span></span>';
} else {
echo '<br><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><br><span itemprop="average"><font face="arial" color="#000000" style="font-size: 0.95em"><?php echo $average_not_rounded; ?></font></span> <font face="arial" color="#000000" style="font-size: 0.95em">out of</font> <span itemprop="best"><font face="arial" color="#000000" style="font-size: 0.95em">5 Stars</font></span></span>';
}
?>



</body>
</html>
Reply
#6

First, I want to thank you for your help, your time, and a great script!
Next...I Got it :-)
This works for me:

<div itemscope itemtype="https://data-vocabulary.org/Review-aggregate">
<span itemprop="itemreviewed"><p align="right" style="line-height: 1.25em; padding-top:4px;"><font style='font-size:1.15em; font-family: arial; color:#000000;'>****</font><font style='font-size:1.15em; font-family: arial; color:#000000;'><br>****</span>
<span itemprop="rating" itemscope itemtype="https://data-vocabulary.org/Rating">

<?php $cmtx_identifier = 'Location';
$cmtx_path = '';
require_once $cmtx_path . 'includes/db/connect.php';
$cmtx_identifier = mysqli_real_escape_string($cmtx_link, $cmtx_identifier);
$page_query = mysqli_query($cmtx_link, "SELECT `id` FROM `" . $cmtx_mysql_table_prefix . "pages` WHERE `identifier` = 'Location'");
$page = mysqli_fetch_assoc($page_query);
$id = $page['id'];
$result = mysqli_query($cmtx_link, "SELECT AVG(`rating`)
FROM (
SELECT `rating` FROM `" . $cmtx_mysql_table_prefix . "comments` WHERE `is_approved` = '1' AND `rating` != '0' AND `page_id` = '10001' AND reply_to = '0'
UNION ALL
SELECT `rating` FROM `" . $cmtx_mysql_table_prefix . "ratings` WHERE `page_id` = '10001'
)
AS `average`
");
$votes = mysqli_num_rows($result);
$average = mysqli_fetch_assoc($result);
$average = $average["AVG(`rating`)"];
$average = round($average, 0);
if ($average == 1) {
echo '<br><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png">';
} elseif ($average == 2) {
echo '<br><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><br><span itemprop="average"><font face="arial" color="#000000" style="font-size: 0.95em"><?php echo $average_not_rounded; ?></font></span> <font face="arial" color="#000000" style="font-size: 0.95em">out of</font> <span itemprop="best"><font face="arial" color="#000000" style="font-size: 0.95em">5</font></span></span>';
} elseif ($average == 3) {
echo '<br><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><br><span itemprop="average"><font face="arial" color="#000000" style="font-size: 0.95em"><?php echo $average_not_rounded; ?></font></span> <font face="arial" color="#000000" style="font-size: 0.95em">out of</font> <span itemprop="best"><font face="arial" color="#000000" style="font-size: 0.95em">5</font></span></span>';
} elseif ($average == 4) {
echo '<br><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png">';
} else {
echo '<br><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png"><img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png">';
}
?>

<br>

<?php
$link = mysql_connect("****", "****", "****");
mysql_select_db("****", $link);
$query = "SELECT ROUND(AVG(rating),1) FROM comments WHERE `is_approved` = '1' AND `rating` != '0' AND `page_id` = '10001' AND reply_to = '0'";
$result = @mysql_query ($query);
$row = mysql_fetch_array($result);
echo "<font style='font-size:0.95em; font-family: arial; color:#000000;'>$row[0] out of 5</font>";
?>

More stuff down here....
Reply


Possibly Related Threads…
Thread / Author Replies Views Last Post
Last Post by cecilia
15-Jun-2014, 01:08 AM
Last Post by DuO-VB
21-Mar-2014, 04:26 PM
Last Post by stariocek
29-Aug-2011, 06:09 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)