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
#1

Hello,

I am not good in php but try to Show the Star Ratig on another Webpage.

I did find this: http://www.commentics.org/forum/showthread.php?tid=663
But I was not able to get it work.

I added this Code to a Webpage with a Productlist:
PHP Code:
<?php
$cmtx_page_id
= '1'; //change this

define('IN_COMMENTICS', '1');
require
'../comments/includes/db/connect.php'; //you *may* need to change this

$cmtx_page_id = htmlspecialchars($cmtx_page_id, ENT_QUOTES, 'UTF-8');
$cmtx_page_id = mysql_real_escape_string($cmtx_page_id);

$query = mysql_query("SELECT `id` FROM `" . $cmtx_mysql_table_prefix . "pages` WHERE `page_id` = '$cmtx_page_id'");
$result = mysql_fetch_assoc($query);
$id = $result['id'];

echo
cmtx_average_rating($id);
?>


But after uploading it does not Show the Rating.

On my Comments-Pages I use the following Code:
PHP Code:
<?php
$cmtx_identifier
= '1';
$cmtx_reference = 'Page One';
$cmtx_path = '../../comments/';
define('IN_COMMENTICS', 'true'); //no need to edit this line
require $cmtx_path . 'includes/commentics.php'; //no need to edit this line
?>

May I have made a mistake with $cmtx_page_id ? I added the number of $cmtx_identifier from the Comments-Page ?

I hope you understand what I want to do? My english is also not so good like my php. Rolleyes

And I hope someone can help me.
Reply
#2

Hi,

Try this:

PHP Code:
<?php
$cmtx_identifier
= '1'; // Change this for each of your pages.

define('IN_COMMENTICS', 'true');

require_once
"comments/includes/db/connect.php"; // You may need to adjust this path.

$cmtx_identifier = mysql_real_escape_string($cmtx_identifier);

$page_query = mysql_query("SELECT `id` FROM `" . $cmtx_mysql_table_prefix . "pages` WHERE `identifier` = '" . $cmtx_identifier . "'");
$page = mysql_fetch_assoc($page_query);
$id = $page["id"];

$result = mysql_query("SELECT AVG(rating) FROM `" . $cmtx_mysql_table_prefix . "comments` WHERE `is_approved` = '1' AND `rating` != '0' AND `page_id` = '$id'");

$average = mysql_fetch_assoc($result);

$average = round($average["AVG(rating)"] / 0.5) * 0.5;

echo
$average;
?>

Have you completed the interview?
Reply
#3

Thank you for answering.

I added the code and uploaded.

It is showing -Access Denied.-

May have to Change some permissios per FTP? Wich ones?

May be importand to know I just upgradet to the Version 2.5.
Reply
#4

permissions on connect.php is 644.

Is that OK?
Reply
#5

I did the following.

I changed this part of the Code:
$cmtx_identifier = '1'; // Change this for each of your pages.
define('IN_COMMENTICS', 'true');

to:
$cmtx_identifier = '1'; // Change this for each of your pages.
$cmtx_path = 'comments/';
define('IN_COMMENTICS', 'true');


And Jet the "access denied" is gone.
But it does Show only a "0" . No Stars. It should Show 3 Stars for $cmtx_identifier = '1' .

I would be so happy if you coud help me again Steven .
Reply
#6

Hi,

If you're using v2.5 then you need to change the MySQL functions to MySQLi.

I can reply again tomorrow with the code but if you want to do it yourself in the meantime then download the 'Recent Comments' add-on from the add-ons page (it uses MySQLi) and the code there should give you an idea of what you need to do.

Have you completed the interview?
Reply
#7

OK thanks.

My PHP knowledge is not very good, but I will download the add-on and install it.
Then I will try to look at the code and try to do it myself.

I will post tomorow, what I have done.
Reply
#8

Hello,

I have integrated the "recent Comments" code into my Website. And I deleted most of the code so I have only this:

PHP Code:
<?php
$cmtx_path
= 'comments/';
require_once
$cmtx_path . 'includes/db/connect.php';

// Start of 'Best Rated' **************************************************************************************************
echo "<h3>Am besten bewertete Depots</h3>";

$comments = mysqli_query($cmtx_link, "SELECT `page_id`, AVG(`rating`) AS `average` FROM `" . $cmtx_mysql_table_prefix . "comments` WHERE `is_approved` = '1' AND `rating` != '0' GROUP BY `page_id` ORDER BY `average` DESC LIMIT 5");

while (
$comment = mysqli_fetch_assoc($comments)) {

$average = round($comment["average"] / 0.5) * 0.5;

$page_query = mysqli_query($cmtx_link, "SELECT * FROM `" . $cmtx_mysql_table_prefix . "pages` WHERE `id` = '" . $comment["page_id"] . "'");
$page = mysqli_fetch_assoc($page_query);

echo
"<a href='" . $page["url"] . "'>" . $page["reference"] . "</a> (" . $average . "/5)";
echo
"<br/>";

}
// End of 'Best Rated' ****************************************************************************************************
?>


There are some similaryties with this:
PHP Code:
<?php
$cmtx_identifier
= '1'; // Change this for each of your pages.

define('IN_COMMENTICS', 'true');

require_once
"comments/includes/db/connect.php"; // You may need to adjust this path.

$cmtx_identifier = mysql_real_escape_string($cmtx_identifier);

$page_query = mysql_query("SELECT `id` FROM `" . $cmtx_mysql_table_prefix . "pages` WHERE `identifier` = '" . $cmtx_identifier . "'");
$page = mysql_fetch_assoc($page_query);
$id = $page["id"];

$result = mysql_query("SELECT AVG(rating) FROM `" . $cmtx_mysql_table_prefix . "comments` WHERE `is_approved` = '1' AND `rating` != '0' AND `page_id` = '$id'");

$average = mysql_fetch_assoc($result);

$average = round($average["AVG(rating)"] / 0.5) * 0.5;

echo
$average;
?>

And, I think there needs to be implemented in a code that the $cmtx_identifier can be taken from MSQLi but i am not able to do this with my php knowledge.
Reply
#9

Hi,

The following code is working for me on v2.5.

For the $cmtx_identifier variable, make sure to enter the same value as in your integration code. Don't enter the value that is in the URL when editing the page in Manage -> Pages in the admin panel.

PHP Code:
<?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`
"
);

$average = mysqli_fetch_assoc($result);

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

$average = round($average, 0);

echo
$average;
?>

Have you completed the interview?
Reply
#10

Thank you so much Steven.
That works very good.
It is showing me the avarage as a number.

I have coded something that Shows the amount of stars.

I did the following with php if and else:

PHP Code:
<?php 
if ($average == 0.5) {
echo
'<img src="comments/images/stars/star_half.png">';
} elseif (
$average == 1) {
echo
'<img src="comments/images/stars/star_full.png">';
} elseif (
$average == 1) {
echo
'<img src="comments/images/stars/star_full.png">';
} elseif (
$average == 1.5) {
echo
'<img src="comments/images/stars/star_full.png"><img src="comments/images/stars/star_half.png">';
} elseif (
$average == 2) {
echo
'<img src="comments/images/stars/star_full.png"><img src="comments/images/stars/star_full.png">';
} elseif (
$average == 2.5) {
echo
'<img src="comments/images/stars/star_full.png"><img src="comments/images/stars/star_full.png"><img src="comments/images/stars/star_half.png">';
} 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">';
} elseif (
$average == 3.5) {
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_half.png">';
} 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">';
} elseif (
$average == 4.5) {
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_half.png">';
} 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"><img src="comments/images/stars/star_full.png">';
}
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)