Jump to content


Photo

5 Star Rating System for Downloadable files.


  • Please log in to reply
2 replies to this topic

#1 Chesso

Chesso
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 24 May 2006 - 07:40 AM

Iv'e been trying to make a basic rating system that allows a user to click on one of five images, each representing a number from 1 to 5 (1,2,3,4 or 5) and somehow calculate a 1-5 rating from the results.

I have tried a few things (although scarce from my findings) but nothing actually works as it is intended.

So I have a simpler idea but need help, basically in my db/table i'm going to have an entry for each rating, for example: rate_1, rate_2, rate_3, rate_4 and rate_5. If a user pressed the image that corresponds to a rating of 3 then rate_3 will go up by one.

The only way I can think of using this data is to find out which of those rate_x happens to have the highest number, so if rate_3 was the highest out of the lot then the rating of the particular file would be 3.

Iv'e tryed using the MAX() php function but this is rather useless to me, I need to know *which* field was the highest out of the rate_x's not have the *actual* highest value returned.

Is there any function that exists or a query that can tell me which field has the highest value? or perhaps there is another way of going about this?

#2 eves

eves
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 24 May 2006 - 02:46 PM

$result = mysql_query("SELECT rate_1, rate_2, rate_3, rate_4, rate_5 FROM (TABLE) WHERE (CONDITION)  ");
$row = mysql_fetch_row($result); // use mysql_fetch_row to get an array with a numeric index

// get the max value in the fields (rate_1, teate_2, rate....) and search for it in the array $row
// array_keys will return an array based on the search made, thus you access it as $index[0]
$index = array_keys($row,max($row)); 

//determine which field is the highest
//order on this part depends on the order of the fields in the select query
switch ($index[0])
{
    case 0:
        //rate_1 is the highest
        break;
    case 1:
        //rate_2 is the highest
        break;
    case 2:
        //rate_3 is the highest
        break;
    case 3:
        //rate_4 is the highest
        break;    
    default:
        //rate_5 is the highest
        break;
}

hope that helps

#3 Chesso

Chesso
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 24 May 2006 - 02:55 PM

Thanks alot!!! :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users