douguazhi5966 2012-10-28 12:55
浏览 16

使用jquery,php和red bean检查用户会话

I've spent more than month trying to make rate system , I really start boring about using jquery :(

I've implemented this tutorial however, this tutorial allows anyone to vote for unlimited time

I successfully join this rating system with mysql DB .. but what I want and I couldn't reach are :

1- check the $_session , if the user does'n a logged in user , prevent him/her from voting . they can just see number of votes .

2- if the user is a logged-in , check if he/she has voted before. If yes then prevent him/her from voting. If no then ALLOW him/her to vote only one time for each product .

funcations.js

    //--------------------------------rating--------------------------------
$(function() { 
 $('.rate_widget').each(function(i) {
            var widget = this;
            var out_data = {
                widget_id : $(widget).attr('id'),
                fetch:1
            };
            $.post(
                'ratings.php',
                out_data,
                function(INFO) {
                    $(widget).data( 'fsr', INFO );
                    set_votes(widget);
                },
                'json'
            );
        });


        $('.ratings_stars').hover(
            // Handles the mouseover
            function() {
                $(this).prevAll().andSelf().addClass('ratings_over');
                $(this).nextAll().removeClass('ratings_vote'); 
            },
            // Handles the mouseout
            function() {
                $(this).prevAll().andSelf().removeClass('ratings_over');
                // can't use 'this' because it wont contain the updated data
                set_votes($(this).parent());
            }
        );





        // This actually records the vote
        $('.ratings_stars').bind('click', function() {
            var star = this;
            var widget = $(this).parent();

            var clicked_data = {
                clicked_on : $(star).attr('class'),
                widget_id : $(star).parent().attr('id')
            };
            $.post(
                'ratings.php',
                clicked_data,
                function(INFO) {
                    widget.data( 'fsr', INFO );
                    set_votes(widget);
                },
                'json'
            ); 
        });








}); 

     function set_votes(widget) {

        var avg = $(widget).data('fsr').whole_avg;
        var votes = $(widget).data('fsr').number_votes;
        var exact = $(widget).data('fsr').dec_avg;



        $(widget).find('.star_' + avg).prevAll().andSelf().addClass('ratings_vote');
        $(widget).find('.star_' + avg).nextAll().removeClass('ratings_vote'); 
        $(widget).find('.total_votes').text( votes + ' votes recorded (' + exact + ' rating)' );
        }
        //--------------------------------END rating--------------------------------

ratings.php

<?php
include('functions.php');
require('rb.php');
    R::setup('mysql:host=localhost;  
       dbname=eshop','root','');



     $rating = new ratings($_POST['widget_id']);


    isset($_POST['fetch']) ? $rating->get_ratings() : $rating->vote();  


      class ratings {


    private $widget_id;
    private $data = array();


function __construct($wid) {

    $this->widget_id = $wid;

    $this->data=R::getAll('select id,number_votes,total_points,dec_avg,whole_avg from product' );


}
public function get_ratings() {

    if (!empty($this->data[$this->widget_id])) {
        echo json_encode($this->data[$this->widget_id]);

    }
    else {
        $data['id'] = $this->widget_id;
        $data['number_votes'] = 0;
        $data['total_points'] = 0;
        $data['dec_avg'] = 0;
        $data['whole_avg'] = 0;
        echo json_encode($data);
    } 
}
public function vote() {

    # Get the value of the vote
    preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
    $vote = $match[1];

    $ID = $this->widget_id;
    # Update the record if it exists
    if (!empty($this->data[$this->widget_id])){
        $this->data[$ID]['number_votes']+= 1;
        $this->data[$ID]['total_points']+= $vote;
    }
    # Create a new one if it doesn't
    else {
        $this->data[$ID]['number_votes'] = 1;
        $this->data[$ID]['total_points'] = $vote;
    }

    $this->data[$ID]['dec_avg'] = round( $this->data[$ID]['total_points'] / $this->data[$ID]['number_votes'], 1 );
    $this->data[$ID]['whole_avg'] = round( $this->data[$ID]['dec_avg'] );

$number_votes=$this->data[$ID]['number_votes'];
$total_points= $this->data[$ID]['total_points'];
$dec_avg=$this->data[$ID]['dec_avg'];
 $whole_avg= $this->data[$ID]['whole_avg'];   


    $save_vote=R::exec('update product set number_votes= :number_votes ,total_points= :total_points ,dec_avg= :dec_avg ,whole_avg= :whole_avg  where id= :ID',
array(':number_votes'=>$number_votes,':total_points'=>$total_points,':dec_avg'=>$dec_avg,':whole_avg'=>$whole_avg,':ID'=>$ID)   );


    $this->get_ratings();
}


}
?>

index.php

function get_new_arrivals(){
$data=R::getAll('select * from product');

foreach ($data as $list)
{
     echo  "<div class='span-6 p_box' ><a href='product_single.php?id=".$list['id']."'><img src='".$list["p_pic"]."'/></a><br/><b>".$list["p_name"]."</b><br/><label>Rent Price: $</label>".$list["p_r_price"]."<br/><label>Buying Price: $</label>".$list["p_b_price"]."<br/>
     <div class='p_choice'>  
    <label>Rate:</label>
    <div id='".$list['id']."' class='rate_widget'>  
        <div class='star_1 ratings_stars'></div>  
        <div class='star_2 ratings_stars'></div>  
        <div class='star_3 ratings_stars'></div>  
        <div class='star_4 ratings_stars'></div>  
        <div class='star_5 ratings_stars'></div>  
        <div class='total_votes'>vote data</div> ";
        if(isset($_SESSION['u_email'])){
         echo "<input type='hidden' id='check_user' value='".$_SESSION['u_email']."'/>";
         }else{
         echo "<input type='hidden' id='check_user' value='guest'/>";
         }
  echo"  </div>  
</div></div>";
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度