Forum Moderators: coopster

Message Too Old, No Replies

Function within a MYSQL query

         

Flurpal

10:29 pm on Jan 26, 2010 (gmt 0)

10+ Year Member



Hello,

I currently have a database with items. Each item includes a field for a certain number of "thumb up" ratings, and a certain number of "thumb down" ratings. Here, I have a nice script which will take the total number of ups and number of downs, and then convert it into a percent as so:


if ($dn==0) {$finep="100%";$nocon="yes";}
if ($up==0) {$finep="0%";$nocon="yes";}
if ($up==0 && $dn==0){$finep="Not Yet Rated";$nocon="yes";}
if (!$nocon=="yes") {
$toduno = $up + $dn;
$iniper = $up / $toduno;
$finipi = round($iniper,2);
$finep=$finipi * 100 . '%'; // finep is the final percent
}

Basically, if there are no downs, then it sets rating as 100%, vice versa. No ratings, it echos no ratings. Otherwise it continues in the code to find the actual percent.

Now, I've been doing fine with this code as far as displaying the percent itself. However, now I am making a "browse" section of my site where I want to order the results by it's final rating in percent. As in:


SELECT * FROM database ORDER BY [percent].

How can I do this? I definitely can't simply order by the greatest number of thumb ups, I have to combine ups and downs to make a mutual percent. Any quick way to integrate them? Thanks in advance.

jatar_k

4:40 pm on Jan 27, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what about having a static percentage stored in the table for sorting or for quick display?

you could update the calculation every time someone adds a new rating