Rate with stars

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Rate with stars

Post by xisle »

Impress your boss, impress your friends!
Do it like the pros... :wink:

This is a piece of a much larger rating system that I built, so you will need to tweak.
It is a simple switch that attaches a 'star' rating
to an average of all ratings in a mysql database.

Image
Image
Image
Image
Image
Image
Image
Image
Image

Code: Select all

<?php

function getStars($num){

	switch($num){

		case ($num > 4.75):
		   $image="stars-5-0.gif";
		   break;
		   
		case ($num > 4.25):
		   $image="stars-4-5.gif";
		   break;
		   		   
		case ($num > 3.75):
		   $image="stars-4-0.gif";
		   break;		   
		   
		case ($num > 3.25):
		   $image="stars-3-5.gif";
		   break;			   
		   
		case ($num > 2.75):
		   $image="stars-3-0.gif";
		   break;	
		   		   
		case ($num > 2.25):
		   $image="stars-2-5.gif";
		   break;		   

		case ($num > 1.75):
		   $image="stars-2-0.gif";
		   break;

		case ($num > 1.25):
		   $image="stars-1-5.gif";
		   break;
		   
		case ($num > .75):
		   $image="stars-1-0.gif";
		   break;
			
		default:
		    $image="none";
	}
	return $image;
}

include("mysql_config.php");

$query = "SELECT AVG(rating) as avrating, COUNT(*) as count FROM Ratings";
$result = mysql_query($query,$link) or die(mysql_error());
$count = mysql_result($result, "0", "count");
$average = number_format(mysql_result($result, "0", "avrating"), 2);
$star = getStars($average);

if($star != "none"){
   print"<span class="rating">Average reader rating: 
   <img src="stars/".$star."">(".$count." review";
   if($count > 1)
   print"s";
   print")</span>";
}
?>
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

and mysql_config.php should say...
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

I would assume the db connectivity.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

<?
mysql_connect(yadaa, yadaa, yadaa);
mysql_select_db(yadaa);
?>
Image Image
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

Looks nice. May I use it to get rich? 8)

Nah, just kiddin'

===================================
= Yes, I'm trying to get my 101st posting today =
===================================
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

Dale wrote:and mysql_config.php should say...

yeh just database connectivity, sorry for late reply - been on holiday.

suprised amazon.com doesn't mind me linking to their stars :lol:
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

xisle wrote:
Dale wrote:and mysql_config.php should say...

suprised amazon.com doesn't mind me linking to their stars :lol:
Amazon are amazon, they dont gie a monkeys left bum cheek bout what goes on :)
beachguy29588
Forum Newbie
Posts: 4
Joined: Tue Mar 30, 2004 1:00 pm

Post by beachguy29588 »

Derfel Cadarn wrote:Looks nice. May I use it to get rich? 8)

Nah, just kiddin'

===================================
= Yes, I'm trying to get my 101st posting today =
===================================
Nice but you could us boxes or other things for rating people. If you need suggestions please e-mail me.
Post Reply