开发者

Possible to rank the display of database results?

I have a SQL database of website names/links/keywords. I then PHP query the database, and display the results with matching keywords as the name and link.

I would like to implement some kind o开发者_高级运维f ranking system. Ideally it would be a clickable five-star type system (like the netflix ranking system, if you are familiar) where it saves each ranking in the database as a new value.

To clarify.. If I initially searched for 'dogs' and 3 websites returned, the default would be for them to be listed in order of their database id. I would like to be able to click a star and have that become the new determining factor on the next search display.

So if db id #1 had (3) 5-star ratings, and (1) 4-star rating, db id #2 had (8) 5-star ratings, and (1) 3-star rating, and db id #3 had (6) 5-star ratings, the results would be id #2, id #3, id #1.

I'm not sure if this is even possible. If it is, maybe someone can link me to some good reading about it.

In case there is a (semi)-easy solution, here is my current output result from the database query.

<?php
// query database
$query = mysql_query("SELECT * FROM  `database` WHERE  `keys` LIKE  '%$q%'");               
// display query results
while($row = mysql_fetch_array($query))
{
echo "<b>".$row['name']."</b>";
echo "<br/>";
echo '<a href="' . $row['weblink'] . '">' . $row['weblink'] . '</a>'; 
}   
mysql_close($con)
?>


This is actually quite easy, what I would do is save the current rank as well as the number of votes.

Then, you could simply order the table by the rating with this: ORDER BY rating DESC

When something recieves a new rating, you add one to the number of votes and calculate the new rating like so:

$new_rating = ( ( $current_rating * $current_votes ) + $new_vote ) / ($current_votes + 1);

For example:

An item has a current rating of 4 with 10 votes, and somebody gives a rating of 1.

$new_rating = ( ( 4 * 10 ) + 1 ) / ( 11 );

And the new rating is: 3.72


You might consider doing a points-based rating system rather than calculating ratings on the fly if you aren't interested in the number of votes. This would mean you only need to add a single INT column to your results table (or relational table if you must).

1 Star = 1 point, 5 stars = 5 points, or however you see fit. You could also allow points to be subtracted by adding a "This didn't help" button or something similar.

When the user selects a rating, you will simply add the points to the current rating and UPDATE the entry. Then of course add ORDER BY rating DESC when you query the db for results.

There are plenty of scripts out there, just search google for "star ratings php" or "5 star rating php" or even search Stack Overflow. I don't want to endorse any particular one, but they all work pretty much the same way.

EDIT: Note that this simply ranks the result higher or lower, but not based on the search parameters it turned up for. That would be (quite a lot) more complicated and I wasn't thinking of it while I was answering, but hopefully this helps a bit anyways, GL!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜