php picture rating

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
haritha
Forum Newbie
Posts: 2
Joined: Wed Aug 13, 2008 4:52 am

php picture rating

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: php picture rating

Post 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.
Post Reply