0

我已经花了一个多月的时间尝试制作费率系统,我真的开始对使用 jquery 感到厌烦:(

我已经实现了本教程,但是,本教程允许任何人无限制地投票

我用 mysql DB 成功加入了这个评级系统.. 但我想要但我无法达到的是:

1-检查 $_session ,如果用户没有登录用户,请阻止他/她投票。他们只能看到票数。

2-如果用户已登录,请检查他/她之前是否投票过。如果是,则阻止他/她投票。如果否,则允许他/她为每个产品投票一次。

函数.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--------------------------------

评级.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();
}


}
?>

索引.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>";
}
4

0 回答 0