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

Show Rating in another Page
#11

Glad it's working now.

However there are no half ratings in v2.5 so you only need to check for 1,2,3,4 and 5.

Have you completed the interview?
Reply
#12

I did add Rich Snippets to the Code.

Thats what it Looks like:
PHP Code:
<div itemscope itemtype="http://data-vocabulary.org/Review-aggregate">

<
span itemprop="itemreviewed">Item</span><br>
<
span itemprop="rating" itemscope itemtype="http://data-vocabulary.org/Rating">
<?
php
$cmtx_identifier
= '1'; // Change this for each of your pages.

$cmtx_path = 'comments/'; // You may need to change this.

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`
"
);

$average = mysqli_fetch_assoc($result);

$average = $average["AVG(`rating`)"];

$average = round($average, 0);

if (
$average == 1) {
echo
'<img src="comments/images/stars/star_full.png"><span itemprop="average">1</span> von <span itemprop="best">5</span></span>';
} elseif (
$average == 2) {
echo
'<img src="comments/images/stars/star_full.png"><img src="comments/images/stars/star_full.png"><span itemprop="average">2</span> von <span itemprop="best">5</span></span>';
} elseif (
$average == 3) {
echo
'<img src="comments/images/stars/star_full.png"><img src="comments/images/stars/star_full.png"><img src="comments/images/stars/star_full.png"><span itemprop="average">3</span> von <span itemprop="best">5</span></span>';
} elseif (
$average == 4) {
echo
'<img src="comments/images/stars/star_full.png"><img src="comments/images/stars/star_full.png"><img src="comments/images/stars/star_full.png"><img src="comments/images/stars/star_full.png"><span itemprop="average">4</span> von <span itemprop="best">5</span></span>';
} else {
echo
'<img src="comments/images/stars/star_full.png"><img src="comments/images/stars/star_full.png"><img src="comments/images/stars/star_full.png"><img src="comments/images/stars/star_full.png"><img src="comments/images/stars/star_full.png"><span itemprop="average">5</span> von <span itemprop="best">5</span></span>';
}
?>

</span>
</div>

But the Google-Rich-Snippets-testing-tool gives following Error:
Either "count" or "votes" field needs to be present.

How can I get the Count?
Reply
#13

Hi,

I'll reply tomorrow with the code.

Have you completed the interview?
Reply
#14

PHP Code:
<div itemscope itemtype="http://data-vocabulary.org/Review-aggregate">

<
span itemprop="itemreviewed">Item</span><br/>
<
span itemprop="rating" itemscope itemtype="http://data-vocabulary.org/Rating">
<?
php
$cmtx_identifier
= '1'; // Change this for each of your pages.

$cmtx_path = 'upload/'; // You may need to change this.

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 == 1) {
echo
'<img src="' . $cmtx_path . 'images/stars/star_full.png"><span itemprop="average">1</span> von <span itemprop="best">5</span></span>';
} elseif (
$average == 2) {
echo
'<img src="' . $cmtx_path . '/images/stars/star_full.png"><img src="' . $cmtx_path . '/images/stars/star_full.png"><span itemprop="average">2</span> von <span itemprop="best">5</span></span>';
} elseif (
$average == 3) {
echo
'<img src="' . $cmtx_path . '/images/stars/star_full.png"><img src="' . $cmtx_path . '/images/stars/star_full.png"><img src="' . $cmtx_path . '/images/stars/star_full.png"><span itemprop="average">3</span> von <span itemprop="best">5</span></span>';
} elseif (
$average == 4) {
echo
'<img src="' . $cmtx_path . '/images/stars/star_full.png"><img src="' . $cmtx_path . '/images/stars/star_full.png"><img src="' . $cmtx_path . '/images/stars/star_full.png"><img src="' . $cmtx_path . '/images/stars/star_full.png"><span itemprop="average">4</span> von <span itemprop="best">5</span></span>';
} else {
echo
'<img src="' . $cmtx_path . '/images/stars/star_full.png"><img src="' . $cmtx_path . '/images/stars/star_full.png"><img src="' . $cmtx_path . '/images/stars/star_full.png"><img src="' . $cmtx_path . '/images/stars/star_full.png"><img src="' . $cmtx_path . '/images/stars/star_full.png"><span itemprop="average">5</span> von <span itemprop="best">5</span></span>';
}
?>
<br/>
<span itemprop="votes"><?php echo $votes; ?></span>
</div>

Have you completed the interview?
Reply
#15

Thank you very much for your help.
Reply


Possibly Related Threads…
Thread / Author Replies Views Last Post
Last Post by Seven Thunders
21-Aug-2018, 04:05 AM
Last Post by cecilia
15-Jun-2014, 01:08 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)