如何以最简单的方式给图片评分?

我正在做一个教程,在那里你可以上传图片和有关信息到网站。图像将保存到文件夹中,而信息将存储在数据库中。 它有一个函数,用于创建每个上传图像的缩略图。在此缩略图旁边,你可以找到存储在数据库中的该图像的信息。但现在,我想增加一个评级系统。 我已经在txt文件中创建了一个分级系统,可以发送数字(1-5)。我的问题是,什么是最简单的方式来连接评分与每一张图片?我希望评级出现在缩略图旁边,就像图像信息一样。

更新图片上传代码:

<div id="uploadform">
                <form action="laddaupp.php" method="post" enctype="multipart/form-data">
                    <input type="hidden" name="MAX_FILE_SIZE" value="500000" />
                    <label for="file"><strong>Filename:</strong></label>
                    <input type="file" name="file" id="file" />
                    <input type="text" name="uppladdare" placeholder="Uploader:" title="What's your name?"/>
                    <input type="text" name="titel" placeholder="Title" title="Give a title to your piece of work"/>
                    <input type="text" name="history" placeholder="History" title="Tell us the story behind your image. Where is it taken, etc."/>
                    <input type="submit" name="submit" value="Upload" />    
                </form>

 //Send data to database
            if(isset($_POST['submit']))
            {   
$dbcon = mysqli_connect("localhost","user1","test1","tutorial");



$stored_file = $_FILES["file"]["name"];
$file = "";

$query="INSERT INTO mountains (filname, uppladdare, titel, History) 
VALUES ('$stored_file','$_POST[uppladdare]','$_POST[titel]','$_POST[history]')";
if(!mysqli_query($dbcon ,$query));
            }

评分代码:

<form name="rating" id="rating">    
    <div id="rating-area" class="shadow">   

    <img src="stjärna.png" id="thumb1" data-value="1" />
    <img src="stjärna.png" id="thumb2" data-value="2" />
    <img src="stjärna.png" id="thumb3" data-value="3" />
    <img src="stjärna.png" id="thumb4" data-value="4" />
    <img src="stjärna.png" id="thumb5" data-value="5" />

    </div>


</form>
<!--Skickar information till textfilen när man klickar på en stjärna-->
<script>
    jQuery('div#rating-area img').click(function(e){
        var val = jQuery(this).data('value') ;
        console.log(val) ;
        jQuery.post('post.php',{ rating : val },function(data,status){
            console.log('data:'+data+'/status'+status) ;
        }) ;
    }) ;

</script>

You can create another table on the DB related to your current (i suppose) Image table. Let's say ImageRate

Since you send 1-5 value as rate you can build your Table this way

Table ImageRate

ID    Rate    ImageID (FK)
1     3       50
2     5       50
3     2       50

each vote/rate will be a row into ImageRate with a auto-incremental ID, the Rate value and the ImageID as foreign key, which is the primary key of the table Image.

So when a user rates an image you'll have to do a insert into this table this way:

INSERT INTO ImageRate(ID, RATE, ImageID) VALUES(null, ?, ?);

where the first ? will be the rate value and the second ? the image id that has been rated.

then to get the effective rate of a image you can simply do

SELECT AVG(rate) from imagerate where ImageID = 50

NOTE

this is a very basic way to do what you need. However, this table could be extended with few more useful columns, for example a timestamp, the User id, in order to prevent that a user can vote twice, and so on... you need to consider all these things depending on your specific case.

The easyest way is to store the votes in the database. You can connect them to an image by the image's ID.

You have to consider a few things:

  • Do you want to prevent multiple votes from a unique user? -> If yes, you have to store all votes individually
  • Do you only need the AVG of the votes? - If yes (and no for the others), you can store the count of votes and the sum of points. (AVG will be the points / count)
  • Do you want to check the trends? - If yes, you should store all votes individually and a timestamp for each vote.

Personally I do NOT recommend to store the votes in a text file! There are too much problems to solve (concurency for example).