...

View Full Version : rating system



moos3
11-16-2007, 08:27 PM
I'm working on a 5 star/icon rating class I have the following code


class rating {
public $user_id ='';
public $rating = '';

// Get Rating
function getRating($user_id){
$r_sql = "SELECT avg(rating) AS rating FROM user_rating WHERE user_id='$cust_id'";
$r_res = mysql_query($r_sql);
$r_rows = mysql_fetch_assoc($r_res);
$myrating = $r_rows['rating'];
return $myrating;
}
// Set Rating
function setRating($user_id,$rating){
$s_rat = "INSERT INTO user_rating(user_id,rating) VALUES($user_id,$rating)";
$s_res = mysql_query($s_rat);
$msg = 'Rating Saved';
return $msg;
}
}

I'm not sure how to figure out the display of the ascept of it. Right now it just returns the number. Where I'm stuck is how to figure out the .5 I understand the loop for whole numbers but how do I handle 3.5 1.5 4.5 and how does one handle rounding up? Ideas?

moos3
11-16-2007, 08:35 PM
I was thinking some type of switch statment to handle 1 through 5. something like this


switch($rate){
case 1:
$myrating = '<img src="lit_cross.png" /><img src="non_cross.png" /><img src="non_cross.png" /><img src="non_cross.png" /><img src="non_cross.png" />';
break;
case 1.5:
$myrating = '<img src="lit_cross.png" /><img src="half_cross.png" /><img src="non_cross.png" /><img src="non_cross.png" /><img src="non_cross.png" />';
break;
case 2:
$myrating = '<img src="lit_cross.png" /><img src="lit_cross.png" /><img src="non_cross.png" /><img src="non_cross.png" /><img src="non_cross.png" />';
case 2.5:
$myrating = '<img src="lit_cross.png" /><img src="lit_cross.png" /><img src="half_cross.png" /><img src="non_cross.png" /><img src="non_cross.png" />';
break;
......
case 5:
$myrating = '<img src="lit_cross.png" /><img src="lit_cross.png" /><img src="lit_cross.png" /><img src="lit_cross.png" /><img src="lit_cross.png" />';
break;
}

I'm I on the right track?

Majoracle
11-16-2007, 09:52 PM
function getRating($user_id) {
$r_sql = "SELECT avg(rating) AS rating FROM user_rating WHERE user_id='$cust_id'";
$r_res = mysql_query($r_sql);
$r_rows = mysql_fetch_assoc($r_res);
$myrating = number_format($r_rows['rating']);
return $myrating;
}

The "number_format" function will round it up or down if it's a decimal, or do nothing at all if it's a whole number. I use it for my rating system, and it works great. More on it here (http://us.php.net/manual/en/function.number-format.php).

moos3
11-16-2007, 10:28 PM
so that will round up if 4.89 and down if 3.43 ?

Majoracle
11-16-2007, 10:34 PM
yup.

4.89 will print 5 and down if 3.43 will print 3.

moos3
11-16-2007, 10:38 PM
what about 3.5 or anything .5 ? I need to be able to show half ratings.

Majoracle
11-16-2007, 11:00 PM
Oh, I must have missed that part. Easy fix though..


function getRating($user_id) {
$r_sql = "SELECT avg(rating) AS rating FROM user_rating WHERE user_id='$cust_id'";
$r_res = mysql_query($r_sql);
$r_rows = mysql_fetch_assoc($r_res);
$myrating = number_format($r_rows['rating'], 1);

$exp_rating = explode('.', $myrating);
switch ($exp_rating[1]) {
case '0': $myrating = $exp_rating[0]; break;
case '1': $myrating = $exp_rating[0]; break;
case '2': $myrating = $exp_rating[0]; break;
case '3': $myrating = $exp_rating[0].".5"; break;
case '4': $myrating = $exp_rating[0].".5"; break;
case '5': $myrating = $exp_rating[0].".5"; break;
case '6': $myrating = $exp_rating[0].".5"; break;
case '7': $myrating = $exp_rating[0].".5"; break;
case '8': $myrating = $exp_rating[0] + 1; break;
case '9': $myrating = $exp_rating[0] + 1; break;
}
return $myrating;
}

Now it will.

moos3
11-16-2007, 11:14 PM
I get the explode but not the 0-2 and 6-9 when its 0-5 is it that way because of the possiblity of 1,1.5,2,2.5,3,3.5,4,4.5,5

Majoracle
11-16-2007, 11:22 PM
yeah, what it's basically doing is rounding the number to the nearest x or x.5

number_format($r_rows['rating'], 1);

The extra parameter (the 1) tells it to round to the nearest tenths spot, so if it's like "3.73" it'll round to "3.7" or it it's "4.67" it'll round to "4.7" or even whole numbers like "4" to "4.0". Then the switch loop checks what's in the tenths spot and rounds the rating to the nearest x or x.5. So basically, "4.64" would return "4.5" or "4.23" would return "4"

OK, hope you understood that.

moos3
11-16-2007, 11:40 PM
So something like this?


$rating = number_format($r_rows['rating'], 1);
$exp_rating = explode('.', $rating);
switch ($exp_rating[1]) {
case '0': $curating = $exp_rating[0]; break;
case '1': $currating = $exp_rating[0]; break;
case '2': $currating = $exp_rating[0]; break;
case '3': $currating = $exp_rating[0].".5"; break;
case '4': $currating = $exp_rating[0].".5"; break;
case '5': $currating = $exp_rating[0].".5"; break;
case '6': $currating = $exp_rating[0].".5"; break;
case '7': $currating = $exp_rating[0].".5"; break;
case '8': $currating = $exp_rating[0] + 1; break;
case '9': $currating = $exp_rating[0] + 1; break;
}
switch($currating){
case 1:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" />';
break;
case 1.5:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/half_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" />';
break;
case 2:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" />';
break;
case 2.5:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/half_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" />';
break;
case 3:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" />';
break;
case 3.5:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/half_rating.jpg" /><img src="/images/rating/non_rating.jpg" />';
break;
case 4:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/non_rating.jpg" />';
break;
case 4.5:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/half_rating.jpg" />';
break;
case 5:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" />';
break;
default:
$myrating = '<img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" />';
break;
}

Majoracle
11-17-2007, 01:05 AM
Yup, that should work for you.

Edit:

Also, didn't learn this was possible until just a couple of hours ago, you can simplify the switch loop to find the decimal.



$rating = number_format($r_rows['rating'], 1);
$exp_rating = explode('.', $rating);

switch($exp_rating[1]){
case '0': case '1': case '2':
$currating = $exp_rating[0];
break;
case '3': case '4': case '5': case '6': case '7':
$currating = $exp_rating[0].".5";
break;
case '8': case '9':
$currating = $exp_rating[0] + 1;
break;
}

switch($currating){
case 1:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" />';
break;
case 1.5:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/half_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" />';
break;
case 2:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" />';
break;
case 2.5:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/half_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" />';
break;
case 3:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" />';
break;
case 3.5:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/half_rating.jpg" /><img src="/images/rating/non_rating.jpg" />';
break;
case 4:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/non_rating.jpg" />';
break;
case 4.5:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/half_rating.jpg" />';
break;
case 5:
$myrating = '<img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" /><img src="/images/rating/rating.jpg" />';
break;
default:
$myrating = '<img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" /><img src="/images/rating/non_rating.jpg" />';
break;
}

MUCH cleaner.

moos3
11-19-2007, 05:00 AM
heres the solution I came up with


$rate = round(($r_rows['rating']*2),0)/2;

it works perfectly rounding to the nearest half



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum