Page 1 of 1

php picture rating

Posted: Wed Aug 13, 2008 4:59 am
by haritha
how to display the highest rated picture from data base containing ratings of different users for same picture i need some idea regarding this

Code: Select all

<?php 
                           if($_POST)
                           {
                                  extract($_POST);
                             $rate=$_POST["rate"];
                      $image=$_POST["image"];
                     //echo $imageid=$_POST["imageid"];
                    
                    
                        /*$result1=mysql_query("SELECT * FROM netkushi_rating");
                       While($row = mysql_fetch_assoc($result1))
                       {
                       $id=$row["id"];
                       $image=$row["image"];
                
                       }*/
                 $sql = "INSERT INTO netkushi_rating (`image` ,`rate`)VALUES ('$image','$rate')";
                    
                     mysql_query($sql);
                 $v="inserted";
                    
                     }
                     
                     
                    
                    
                    ?>
this code i have used to insert rating of the picture
so i need the help for displaying highest rated picture first in home page
thank u
haritha

Re: php picture rating

Posted: Wed Aug 13, 2008 5:06 am
by onion2k
Assuming that the 'rate' value is numeric and higher is better, you just need to get the one whose total is highest..

Code: Select all

SELECT `netkushi_rating`.`image`, SUM(`netkushi_rating`.`rating`) AS total 
FROM `netkushi_rating`
GROUP BY `netkushi_rating`.`image`
ORDER BY total DESC
LIMIT 1
For the record though, I wouldn't have stored the data that way. It's a bit wasteful if you don't need the individual ratings. If you do need the individual ratings you're not storing the user's id with them. Either way it's not ideal.

Also, you're not escaping your SQL (with mysql_real_escape_string()).

Plus, extract() is a potential security issue.