weixin_33709609 2014-03-31 07:56 采纳率: 0%
浏览 72

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

我正在做一个教程,在那里你可以上传图片和有关信息到网站。图像将保存到文件夹中,而信息将存储在数据库中。 它有一个函数,用于创建每个上传图像的缩略图。在此缩略图旁边,你可以找到存储在数据库中的该图像的信息。但现在,我想增加一个评级系统。 我已经在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>
  • 写回答

2条回答 默认 最新

  • Memor.の 2014-03-31 08:03
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀