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

Moving of the stars to the different place of the same page
#11

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.';
}

Have you completed the interview?
Reply
#12

Yes, Steven
Works great now!
Only does not work ' . $votes . '
$votes = mysqli_num_rows($result); extract value 1 on every page
Reply
#13

How about this?

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

//get comment ratings
$result = mysqli_query($cmtx_link, "SELECT * FROM `" . $cmtx_mysql_table_prefix . "comments` WHERE `page_id` = '$id' AND `rating` != '0' AND `is_approved` = '1'");
$total_1 = mysqli_num_rows($result);

//get guest ratings
$result = mysqli_query($cmtx_link, "SELECT * FROM `" . $cmtx_mysql_table_prefix . "ratings` WHERE `page_id` = '$id'");
$total_2 = mysqli_num_rows($result);

//calculate total
$votes = $total_1 + $total_2;

$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.';
}

Have you completed the interview?
Reply
#14

Yes,
the number of comments now appear correctly, however the stars disappeared. See attached file.


Attached Files Thumbnail(s)
   
Reply
#15

Sorry I think the $result variable was being overwritten:

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

//get comment ratings
$result_1 = mysqli_query($cmtx_link, "SELECT * FROM `" . $cmtx_mysql_table_prefix . "comments` WHERE `page_id` = '$id' AND `rating` != '0' AND `is_approved` = '1'");
$total_1 = mysqli_num_rows($result_1);

//get guest ratings
$result_1 = mysqli_query($cmtx_link, "SELECT * FROM `" . $cmtx_mysql_table_prefix . "ratings` WHERE `page_id` = '$id'");
$total_2 = mysqli_num_rows($result_1);

//calculate total
$votes = $total_1 + $total_2;

$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.';
}

Have you completed the interview?
Reply
#16
Smile 

YES Smile
Great job. Thank you Steven.
Commentics is incredible, your support is perfect tooSmile

It would be great if a in future update you add Facebook, G+ and Twitter logins, also extracting their avatars from the social systems.
Reply


Possibly Related Threads…
Thread / Author Replies Views Last Post
Last Post by Seven Thunders
20-Jun-2019, 03:56 AM
Last Post by DuO-VB
21-Mar-2014, 04:26 PM
Last Post by Steven
01-Feb-2013, 03:48 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)