23-Mar-2014, 11:38 AM
I think there just needs to be a conditional in case there are no ratings yet.
PHP Code:
<?php
$cmtx_identifier = $_GET['id'];
$cmtx_path = 'rev/';
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 = mysqli_fetch_assoc($result);
$average = $average["AVG(`rating`)"];
$average = round($average, 0);
if ($average && $votes) {
echo '<div itemscope itemtype="http://data-vocabulary.org/Review-aggregate">';
echo '<span itemprop="rating" itemscope itemtype="http://data-vocabulary.org/Rating">';
if ($average == 1) {
echo '<img src="/rev/images/stars/star_full.png"><span itemprop="average">1</span>/<span itemprop="best">5</span></span>';
} elseif ($average == 2) {
echo '<img src="/rev/images/stars/star_full.png"><img src="/rev/images/stars/star_full.png"><span itemprop="average">2</span>/<span itemprop="best">5</span></span>';
} elseif ($average == 3) {
echo '<img src="/rev/images/stars/star_full.png"><img src="/rev/images/stars/star_full.png"><img src="/rev/images/stars/star_full.png"><span itemprop="average">3</span>/<span itemprop="best">5</span></span>';
} elseif ($average == 4) {
echo '<img src="/rev/images/stars/star_full.png"><img src="/rev/images/stars/star_full.png"><img src="/rev/images/stars/star_full.png"><img src="/rev/images/stars/star_full.png"><span itemprop="average">4</span>/<span itemprop="best">5</span></span>';
} else {
echo '<img src="/rev/images/stars/star_full.png"><img src="/rev/images/stars/star_full.png"><img src="/rev/images/stars/star_full.png"><img src="/rev/images/stars/star_full.png"><img src="/rev/images/stars/star_full.png"><span itemprop="average">5</span>/<span itemprop="best">5</span></span>';
}
echo ' <span itemprop="votes">' . $votes . '</span>';
echo '</div>';
} else {
echo 'Here is whatever you want to do if the page has no ratings yet.';
}