首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >评分系统辅助

评分系统辅助
EN

Stack Overflow用户
提问于 2013-01-04 07:07:01
回答 4查看 186关注 0票数 0

我使用的是来自http://net.tutsplus.com/tutorials/html-css-techniques/building-a-5-star-rating-system-with-jquery-ajax-and-php/的评分脚本

但我希望它能将评分上传到数据库中,并阻止一个人一直投票给同一张图片。

这是我上传和设置cookie的脚本:

代码语言:javascript
复制
<?php 
// Get id and voted value
$id = $_POST['widget_id'];
preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
$vote = $match[1];

// Connect to database and find the row which have the id
$get = mysql_query("SELECT * FROM ratings WHERE id = '$id'");

while ($getdata = mysql_fetch_array($get)) {
    $total_votes = $getdata['total_votes'];
    $total_value = $getdata['total_value'];

    // See if the votes and value is 0 and if it aint it update the database and if it is were creating a new row
    if ($total_votes != 0 && $total_value != 0) {
            $total_votes++;
            mysql_query("UPDATE ratings SET total_votes = '$total_votes' WHERE 
                         id = '$id'");
} else {
    mysql_query("INSERT INTO ratings (id, total_votes, total_value) 
                 VALUES ('$id', '1', '$vote')");
}

// Sets the cookie
$value = $id;
// Send a cookie that expires in 24 hours
setcookie($id, $value, time() + 3600 * 24); 
?>

但是如果用户已经投票,他仍然可以投票,所以我需要一些方法来检查他是否有cookie,以及从mysql表中获取数据并将其发送给用户的方法。

EN

回答 4

Stack Overflow用户

发布于 2013-01-04 07:09:59

语法不正确...

代码语言:javascript
复制
$total_votes = $total_votes . +1;

这会将1添加到$total_votes

代码语言:javascript
复制
$total_votes++;
票数 2
EN

Stack Overflow用户

发布于 2013-01-04 07:19:56

这里将$value设置为与id相同。

代码语言:javascript
复制
 $value = $id;

在这里,您创建了一个称为变量$id的值的cookie,它创建了cookie的动态名称。

代码语言:javascript
复制
setcookie($id,$value, time()+3600*24); 

要制作cookie,请始终设置静态名称

代码语言:javascript
复制
//create cookie
setcookie('widget_id',$id, time()+3600*24); // $value is useless

//read cookie
echo $_COOKIE['widget_id']; //prints the cookie

// unset cookie
unset($_COOKIE['widget_id'];);
票数 1
EN

Stack Overflow用户

发布于 2013-01-04 09:02:34

好的,我得到了检查cookie是否设置完成的函数,所以现在我只需要从mysql表中获取数据并输出到javascript

以下是我的代码

代码语言:javascript
复制
    <?php 
// Get id and voted value
$id = $_POST['widget_id'];
preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
$vote = $match[1];
// Connect to database and find the row which have the id
$get = mysql_query("SELECT * FROM ratings WHERE id = '$id'");

if (isset($_COOKIE[$id])) {
echo 'cookie exist';
exit;
} else {

while ($getdata = mysql_fetch_array($get)) {
    $total_votes = $getdata['total_votes'];
    $total_value = $getdata['total_value'];

    // See if the votes and value is 0 and if it aint it update the database and if it is were creating a new row
    if ($total_votes != 0 && $total_value != 0) {
            $total_votes++;
            mysql_query("UPDATE ratings SET total_votes = '$total_votes' WHERE 
                         id = '$id'");
} else {
    mysql_query("INSERT INTO ratings (id, total_votes, total_value) 
                 VALUES ('$id', '1', '$vote')");
}

// Send a cookie that expires in 24 hours
setcookie($id, $id, time() + 3600 * 24); 
}
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14148748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档