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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀