Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12

Thread: rating system

  1. #1
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post

    rating system

    I'm working on a 5 star/icon rating class I have the following code
    PHP 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?
    Just a simple Developer and IT student
    My Blog
    My Portfolio
    My Playground of Technology

  • #2
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    I was thinking some type of switch statment to handle 1 through 5. something like this
    PHP Code:
    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?
    Just a simple Developer and IT student
    My Blog
    My Portfolio
    My Playground of Technology

  • #3
    Regular Coder
    Join Date
    Nov 2006
    Posts
    246
    Thanks
    13
    Thanked 26 Times in 24 Posts
    PHP Code:
    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.
    Last edited by Majoracle; 11-16-2007 at 09:20 PM.

  • #4
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    so that will round up if 4.89 and down if 3.43 ?
    Just a simple Developer and IT student
    My Blog
    My Portfolio
    My Playground of Technology

  • #5
    Regular Coder
    Join Date
    Nov 2006
    Posts
    246
    Thanks
    13
    Thanked 26 Times in 24 Posts
    yup.

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

  • #6
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    what about 3.5 or anything .5 ? I need to be able to show half ratings.
    Just a simple Developer and IT student
    My Blog
    My Portfolio
    My Playground of Technology

  • #7
    Regular Coder
    Join Date
    Nov 2006
    Posts
    246
    Thanks
    13
    Thanked 26 Times in 24 Posts
    Oh, I must have missed that part. Easy fix though..

    PHP Code:
    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.
    Last edited by Majoracle; 11-16-2007 at 10:08 PM.

  • #8
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    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
    Just a simple Developer and IT student
    My Blog
    My Portfolio
    My Playground of Technology

  • #9
    Regular Coder
    Join Date
    Nov 2006
    Posts
    246
    Thanks
    13
    Thanked 26 Times in 24 Posts
    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.
    Last edited by Majoracle; 11-16-2007 at 10:32 PM.

  • #10
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    So something like this?
    PHP Code:
    $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;
        } 
    Just a simple Developer and IT student
    My Blog
    My Portfolio
    My Playground of Technology

  • #11
    Regular Coder
    Join Date
    Nov 2006
    Posts
    246
    Thanks
    13
    Thanked 26 Times in 24 Posts
    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.

    PHP Code:
    $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.
    Last edited by Majoracle; 11-17-2007 at 09:26 AM.

  • #12
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    heres the solution I came up with
    PHP Code:
    $rate round(($r_rows['rating']*2),0)/2
    it works perfectly rounding to the nearest half
    Just a simple Developer and IT student
    My Blog
    My Portfolio
    My Playground of Technology


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •