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

1/2 (half) stars?
#1

Hello Steven! It's been a while.
I am currently using the following code to show the average rating. It does show an accurate rating 4.5 stars out of 5 (for example). However, I am having trouble implementing 4 full stars and 1/2 star at the end (4 and 1/2 stars).

Can you see what I may be doing wrong? Thank you kindly in advance.


Code:
<?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` = '****' AND reply_to = '0'";       
$result = @mysql_query ($query); 
$row = mysql_fetch_array($result);
echo "<br><b><font style='font-size:0.98em; font-family: arial; color:#E35000;'>$row[0]</font></b>";
?>

<?php $cmtx_identifier = '****';
$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` = '****'");
$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` = '****' AND reply_to = '0'
    UNION ALL
    SELECT `rating` FROM `" . $cmtx_mysql_table_prefix . "ratings` WHERE `page_id` = '****'
    )
    AS `average`
    ");
$votes = mysqli_num_rows($result);
$average = mysqli_fetch_assoc($result);
$average = $average["AVG(`rating`)"];
$average = round($average, 1);
if ($average == 1) {
    echo ' <img src="' . $cmtx_path . 'https://www.****.com/images/stars/star_full.png">';
} elseif ($average == 2) {
    echo ' <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"></font> <span itemprop="best"><font face="arial" color="#000000" style="font-size: 0.95em"></font></span></span>';
}  elseif ($average == 2.5) {
    echo ' <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-half.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"></font> <span itemprop="best"><font face="arial" color="#000000" style="font-size: 0.95em"></font></span></span>';
}  elseif ($average == 3) {
    echo ' <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"></font> <span itemprop="best"><font face="arial" color="#000000" style="font-size: 0.95em"></font></span></span>';
}  elseif ($average == 3.5) {
    echo ' <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-half.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"></font> <span itemprop="best"><font face="arial" color="#000000" style="font-size: 0.95em"></font></span></span>';
}  elseif ($average == 4) {
    echo ' <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">';
}  elseif ($average == 4.5) {
    echo ' <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-half.png">';
}  else {
    echo ' <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-end.png">';
}
?>
Reply
#2

Actually the code above does work... sort-of. I failed to put incements of 1.2, 1.3, 1.4, etc all the way to 4.9. Everything is fine now.
Reply


Possibly Related Threads…
Thread / Author Replies Views Last Post
Last Post by tykra
13-Dec-2011, 01:27 PM
Last Post by stariocek
29-Aug-2011, 06:09 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)