5星评级系统基于SESSION user_id

时间:2016-02-20 15:39:08

标签: php mysql session rating-system

我有一个简单的大学项目系统,我已经陷入困境,真的很感激一些帮助。我根据用户登录创建了一个5星评级系统,我的项目都将在localhost上运行,但我需要多个人在同一台设备上进行测试。

我的评分系统工作正常并插入我的数据库中,但我希望在多个页面中使用我的评级系统。我有两个文件来完成我的动作E1index.php和我包含的E1ajax.php。

我尝试为其他页面创建单独的索引文件(E2index.php,E3index.php等),以便同一个用户可以对多个页面进行评级,但每个用户只有1个速率插入我的数据库,第二个速率将覆盖首先。

我的问题是,有人可以解释我如何修改我的代码,让我的评级插入到同一用户的单独行中,具体取决于他们所在的页面吗?

E1index.php

<?php  
    session_start();
    include_once 'dbconnect.php';

    if( !isset( $_SESSION['user'] ) ) header("Location: index.php");

    $res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
    $userRow=mysql_fetch_array( $res );

        $post_id = '1'; 
    ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-20" />
<title>Welcome - <?php echo $userRow['username']; ?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
 <div id="left">
    <label>NHSCT eNISAT Tutorials</label>
    </div>
    <div id="right">
     <div id="content">
         Welcome <?php echo $userRow['forename']; ?>&nbsp;<a href="eNISATVids.php?home">Return to Tutorials page</a>&nbsp;&nbsp;<a href="logout.php?logout">Sign Out</a>
        </div>
    </div>
    <br>
    <br>
    <br>
    <br>
    <br>
<p align="center"><img src="title.jpeg" width="400"height="100" alt="title.jpeg">
<br>
  <link rel="stylesheet" href="style.css" type="text/css" />
        <link type="text/css" rel="stylesheet" href="css/example.css">


    <div class="tuto-cnt">

        <div class="rate-ex1-cnt">
            <div id="1" class="rate-btn-1 rate-btn"></div>
            <div id="2" class="rate-btn-2 rate-btn"></div>
            <div id="3" class="rate-btn-3 rate-btn"></div>
            <div id="4" class="rate-btn-4 rate-btn"></div>
            <div id="5" class="rate-btn-5 rate-btn"></div>
        </div>

        <hr>

        <div class="box-result-cnt">
            <?php
                $query = mysql_query("SELECT * FROM enisat_rate"); 
                while($data = mysql_fetch_assoc($query)){
                    $rate_db[] = $data;
                    $sum_rates[] = $data['rate'];
                }
                if(@count($rate_db)){
                    $rate_times = count($rate_db);
                    $sum_rates = array_sum($sum_rates);
                    $rate_value = $sum_rates/$rate_times;
                    $rate_bg = (($rate_value)/5)*100;
                }else{
                    $rate_times = 0;
                    $rate_value = 0;
                    $rate_bg = 0;
                }
            ?>
            <hr>
            <h3>This tutorial has been rated <strong><?php echo $rate_times; ?></strong> times.</h3>
            <hr>
            <h3>The average rating is <strong><?php echo $rate_value; ?></strong> stars.</h3>
            <hr>
            <div class="rate-result-cnt">
                <div class="rate-bg" style="width:<?php echo $rate_bg; ?>%"></div>
                <div class="rate-stars"></div>
            </div>
            <hr>

        </div><!-- /rate-result-cnt -->

    </div><!-- /tuto-cnt -->


    <script>
        // rating script
        $(function(){ 
            $('.rate-btn').hover(function(){
                $('.rate-btn').removeClass('rate-btn-hover');
                var therate = $(this).attr('id');
                for (var i = therate; i >= 0; i--) {
                    $('.rate-btn-'+i).addClass('rate-btn-hover');
                };
            });

            $('.rate-btn').click(function(){    
                var therate = $(this).attr('id');
                var dataRate = 'act=rate&post_id=<?php echo $post_id; ?>&rate='+therate; //
                $('.rate-btn').removeClass('rate-btn-active');
                for (var i = therate; i >= 0; i--) {
                    $('.rate-btn-'+i).addClass('rate-btn-active');
                };
                $.ajax({
                    type : "POST",
                    url : "http://localhost/Tna/E1ajax.php",
                    data: dataRate,
                    success:function(){}
                });

            });
        });
    </script>


</body>
</html>

E1ajax.php

<?php

session_start();
include_once 'dbconnect.php';

if(!isset($_SESSION['user']))
{
 header("Location: index.php");
}
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);

    if($_POST['act'] == 'rate'){
        //search if the userID has already gave a note
        $userID=$_SESSION['user'];
        $therate = $_POST['rate'];
        $thepost = $_POST['post_id'];

        $query = mysql_query("SELECT * FROM enisat_rate where user_id= '$userID'"); 
        while($data = mysql_fetch_assoc($query)){
            $rate_db[] = $data;
        }

        if(@count($rate_db) == 0 ){
            mysql_query("INSERT INTO enisat_rate (id_post, user_id, rate)VALUES('$thepost', '$userID', '$therate')");
        }else{
            mysql_query("UPDATE enisat_rate SET rate= '$therate' WHERE user_id = '$userID'");
        }
    } 
?>

0 个答案:

没有答案
相关问题