如何为来自不同用户帐户的项目评分

时间:2015-01-23 10:20:17

标签: php mysql

我设计了一个在线购物网站。

现在我想实施一个基于评级的系统,用户可以对不同的产品进行评级。

我已经以这样的方式编写代码,即从单个IP开始,速率只能被接受一次。这工作正常。

但后来我认为其他用户可能会从同一个IP登录。在这种情况下,系统应检查具有特定id的用户是否已经投票。

我被困在这里。

如果一个用户对该项目进行评级,那么他的id将存储在数据库中。接下来当具有不同用户ID的另一个用户登录他的id时没有被存储,或者更确切地说如何在数据库中存储多个用户ID,并检查他们是否先前已经对该项目进行了评级。我的数据库设计如下:

表名:评分

列-----

id(自动增量),user_id(int),total_votes(int 5),total_value(int 5),used_ips(长文本),date,product_id(varchar 20)

<?php
    error_reporting(0);
    include_once 'dbcon.php';
    $prod_id=$_SESSION['rateproid'];
    $userid_rating=$_SESSION['ud'];
    //echo $prod_id;

    // Get next auto increment value
    $result = mysql_query("SHOW TABLE STATUS LIKE 'ratings'");
    $row = mysql_fetch_array($result);
    $nextId = $row['Auto_increment'];   
?>


<?php
    header("Cache-Control: no-cache");
    header("Pragma: nocache");
    include_once 'settings.php';
    //$id_sent = preg_replace("/[^0-9]/","",$_REQUEST['id']);
    //echo $id_sent;
    $id_sent=$nextId;
    $vote_sent = preg_replace("/[^0-9]/","",$_REQUEST['stars']);
    $ip =$_SERVER['REMOTE_ADDR'] ;
    connect();
    $q=mysql_num_rows(mysql_query("select id from ratings where product_id='$prod_id'"));
    //$q="SELECT count(*) from ratings where product_id='$prod_id'";
    //echo $q;
    echo $userid_rating;

    if(!$q)
        mysql_query("insert into ratings (id,date) values    ($id_sent,curdate())");

    if ($vote_sent > $units) 
        die("Sorry, vote appears to be invalid."); // kill    the script because normal users will never see this.

    //connecting to the database to get some information
    $query = mysql_query("SELECT total_votes, total_value, used_ips FROM  $rating_dbname.$rating_tableName WHERE product_id='$prod_id' ")or die(" Error: ".mysql_error());
    $numbers = mysql_fetch_assoc($query);
    $checkIP = unserialize($numbers['used_ips']);
    $count = $numbers['total_votes']; //how many votes total
    $current_rating = $numbers['total_value']; //total number of rating added     together and stored
    $sum = $vote_sent+$current_rating; // add together the current vote value  and the total vote value
    $tense = ($count==1) ? "vote" : "votes"; //plural form votes/vote

    // checking to see if the first vote has been tallied
    // or increment the current number of votes
    ($sum==0 ? $added=0 : $added=$count+1);

    // if it is an array i.e. already has entries the push in another value
    ((is_array($checkIP)) ? array_push($checkIP,$ip) : $checkIP=array($ip));
    $insertip=serialize($checkIP);

    //IP check when voting

    if(!isset($_COOKIE['rating_'.$id_sent])) {
        //$voted=mysql_num_rows(mysql_query("SELECT used_ips FROM    $rating_dbname.$rating_tableName WHERE used_ips LIKE '%".$ip."%' AND   product_id='".$prod_id."' "));

        $voted=mysql_num_rows(mysql_query("SELECT user_id FROM   $rating_dbname.$rating_tableName WHERE user_id='".$userid_rating."' AND  product_id='".$prod_id."' "));
    } else 
        $voted=1;

    if(!$voted) {     //if the user hasn't yet voted, then vote normally...
        if (($vote_sent >= 1 && $vote_sent <= $units)) { // keep votes within range, make sure IP matches   
            $update = "UPDATE $rating_tableName SET total_votes='".$added."',  total_value='".$sum."', used_ips='".$insertip."', product_id='".$prod_id."',  user_id='".$userid_rating."' WHERE id='$id_sent'";
            $result = mysql_query($update); 
            if($result) 
                setcookie("rating_".$id_sent,1, time()+ 2592000);
        } 
    } //end for the "if(!$voted)"
    // these are new queries to get the new values!
    $newtotals = mysql_query("SELECT total_votes, total_value, user_id FROM   $rating_tableName WHERE product_id='$prod_id' ")or die(" Error: ".mysql_error());
    $numbers = mysql_fetch_assoc($newtotals);
    $count = $numbers['total_votes'];//how many votes total
    $current_rating = $numbers['total_value'];//total number of rating added  together and stored
    $tense = ($count==1) ? "vote" : "votes"; //plural form votes/vote

    // $new_back is what gets 'drawn' on your page after a successful 'AJAX/Javascript' vote
    if($voted) {
        $sum=$current_rating; $added=$count;
    }
    $new_back = array();
    for($i=0;$i<5;$i++) {
        $j=$i+1;
        if($i<@number_format($current_rating/$count,1)-0.5) 
            $class="ratings_stars ratings_vote";
        else 
            $class="ratings_stars";

        $new_back[] .= '<div class="star_'.$j.' '.$class.'"></div>';
    }

    $new_back[] .= ' <div class="total_votes"><p class="voted"> Rating:   <strong>'.@number_format($sum/$added,1).'</strong>/'.$units.' ('.$count.' '.$tense.' cast) ';
    if(!$voted)
        $new_back[] .= '<span class="thanks">Thanks for voting!</span> </p>';
    else {
        $new_back[] .= '<span class="invalid">Already voted for this   item</span></p></div>';
    }
    $allnewback = join("\n", $new_back);

    // ========================

    $output = $allnewback;
    echo $output;
?>

0 个答案:

没有答案