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

sql to get star rating for specific entry
#1

Hi
I want to select the highest star rating from my database for a group of records of specific page egSadcafe_1) so somthing like this happens:
mysql_query("SELECT Rating WHERE id =("cafe_1") COUNT(page_id) AS total FROM =("cafe_1") ORDER BY 'total' DESC LIMIT 1"");
I hope you understand what I mean.....
Any help appreciated.
thanks
Peter
Reply
#2

Hi,

You mean something like this:

mysql_query("SELECT MAX(rating) FROM comments WHERE is_approved = '1' AND rating != '0' AND page_id = '1');

Have you completed the interview?
Reply
#3

Hi Steven,
yes I guess but I want it to display the best rating on a page that has
a list of the pages with the comments on.So the page is like this but the comments themselves are on the cafe1.php cafe1.php cafe1.php pages
Does this seem right?
<body>
<div id="content">
<?php include("../content/cafe1.php"); ?>
$rating_cafe_1 =mysql_query("SELECT MAX(rating) FROM comments WHERE is_approved = '1' AND rating != '0' AND page_id = 'cafe_1');
echo $"[rating_cafe_1]"
</div>
<div id="content1">
<?php include("../content/cafe2.php"); ?>
$rating_cafe_2 =mysql_query("SELECT MAX(rating) FROM comments WHERE is_approved = '1' AND rating != '0' AND page_id = 'cafe_2');
echo $"[rating_cafe2]"
</div>
<div id="content2">
<?php include("../content/cafe3.php"); ?>
$rating_cafe_3' =mysql_query("SELECT MAX(rating) FROM comments WHERE is_approved = '1' AND rating != '0' AND page_id = 'cafe_3');
echo $"[rating_cafe_3]"
</div>
</body>
Thanks for your help
Peter
Reply
#4

So you want a list of the 'best rated' pages, like this:

Best Rated

Page G is rated 5
Page F is rated 4.5
Page T is rated 3
Page K is rated 3
Page D is rated 1

Have you completed the interview?
Reply
#5

Hello Steven,
I am working on restaurants, as some towns have more than 1 restaurant
I have individual restaurant pages with a comments form on.
I have a town page with a description, an include from the all restaurant pages in that town. I want to add the rating of each restaurant to this Town page and if the restaurant is not yet rated display "waiting for a rating".
hope this is clear.
restos.jpg attatched
thanks for your help
Peter


Attached Files Thumbnail(s)
   
Reply
#6

Hi Steven.
here is updated jpg of what i want my page to look like. But I cant get code right.
This is code I am using:
<?php
define ('IN_COMMENTICS', '1');
require "../comments/includes/db/connect.php";
$rating_cagarol = mysql_query("SELECT MAX(rating) FROM comments WHERE is_approved = '1' AND rating != '0' AND page_id = 'cagarol.php'");

{
echo $rating_cagarol["rating_cagarol"];
}
?>

Thanks for any help
peter


Attached Files Thumbnail(s)
   
Reply
#7


Hi Steven,
will this take care of the display of the stars. How do I tie this in with the sql query
case "1":
echo"Highest rating";
echo "<img src=\"../images/star1.png\"/>";
break;
case"2":
echo"Highest rating";
echo "<img src=\"../images/star2.png\"/>";
break;
case"3":
echo"Highest rating";
echo "<img src=\"../images/star3.png\"/>";
break;
case"4":
echo"Highest rating";
echo "<img src=\"../images/star4.png\"/>";
break;
case"5":
echo"Highest rating";
echo "<img src=\"../images/star5.png\"/>";
break;
default:
echo "Waiting for a star rating";
break;

thanks
Peter
Reply
#8

Hi,

Try this code and see if it's what you want.

PHP Code:
<?php 
$query
= mysql_query("SELECT MAX(rating) AS highest_rating FROM comments WHERE is_approved = '1' AND page_id = 'cagarol.php'");
$result = mysql_fetch_assoc($query);
$highest_rating = $result["highest_rating"];

switch (
$highest_rating) {
case
0:
echo
"Waiting for a star rating";
break;
case
1:
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_empty.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_empty.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_empty.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_empty.png'/>";
break;
case
1.5:
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_half.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_empty.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_empty.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_empty.png'/>";
break;
case
2:
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_empty.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_empty.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_empty.png'/>";
break;
case
2.5:
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_half.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_empty.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_empty.png'/>";
break;
case
3:
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_empty.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_empty.png'/>";
break;
case
3.5:
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_half.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_empty.png'/>";
break;
case
4:
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_empty.png'/>";
break;
case
4.5:
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_half.png'/>";
break;
case
5:
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
echo
"<img src='http://www.domain.com/comments/images/stars/star_full.png'/>";
break;
}

Have you completed the interview?
Reply
#9

Hi Steven
I Inserted code on my php page.
and I am getting errors on my page.
Warning: mysql_query() [function.mysql-query]: Access denied for user 'a7DELETED/'@'localhost' (using password: NO) in /home/a7DELETED/public_html/villages/aigne.php on line 93

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a7DELETED/public_html/villages/aigne.php on line 94
Thanks for your help
Peter

Reply
#10

Did you connect to the database first?

PHP Code:
<?php 
define
('IN_COMMENTICS', '1');
require
"../comments/includes/db/connect.php";

Have you completed the interview?
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
Last Post by DuO-VB
21-Mar-2014, 04:26 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)