Jump to content

Recommended Posts

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?
[code]
$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;
}
[/code]

hope that helps
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.