Php mysql用户评级系统问题与评级不同的餐馆

时间:2016-09-16 20:27:55

标签: javascript php jquery

我在用户评级系统中遇到问题。一旦我评价一家餐馆并去另一家餐馆,我无法评价该餐厅,它会被禁用。

我注意到 index.php echo starBar(5, 103, 16);中间的 103 mediaId ,这有问题当我改变这个id没有并去投票然后它工作,一旦我再次评价这个媒体id没有人可以在任何地方评价。

完整的代码如下:

的index.php

<?php
$getRest    = $_GET['sid'];

function starBar($numStar, $mediaId, $starWidth) { // function with arguments: number of stars, media ID, width of the star image
    global $bdd;
    $cookie_name = 'tcRatingSystem'.$mediaId; // Set up the cookie name
    $sid    = mysql_real_escape_string($_GET['sid']);
    // We get the rate average and number of rate from the database
    $query = $bdd->getOne('SELECT round(avg(rate), 2) AS average, count(rate) AS nbrRate, sr_id AS sr_id FROM rest_rating WHERE media='.$mediaId.' and sr_id = "'.$sid.'"');
    $avgCeil = round($query['average'], 0); // round above or below to show how many selected stars we display

    $getJSON = array('numStar' => $numStar, 'mediaId' => $mediaId); // We create a JSON with the number of stars and the media ID
    $getJSON = json_encode($getJSON);

    // We create the DIV block with selected stars and unselected stars depending of the rate
    $starBar = '<div id="'.$mediaId.'">';
    $starBar .= '<div class="';
    if( !isset($_COOKIE[$cookie_name]) ) $starBar .= 'star_bar';
    $starBar .= '" rel='.$getJSON.' style="width:'.($numStar*$starWidth).'px">';

    for ($i=1; $i<=$numStar; $i++) {
        $starBar .= '<div class="';
        if ($i <= $avgCeil) $starBar .= 'star_selected'; else $starBar .= 'star';
        $starBar .= '"></div>';
    }
    $starBar .= '</div>';
    $starBar .= '<div class="resultMedia'.$mediaId.'" style="font-size: small; color: grey">'; // We show the rate score and number of rates
    if ($query['nbrRate'] == 0) $starBar .= 'Not rated yet';
    else $starBar .= 'Rating: ' . $query['average'] . '/' . $numStar . ' (' . $query['nbrRate'] . ' votes)';
    $starBar .= '</div>';
    $starBar .= '<div class="box'.$mediaId.'"></div>'; // Return the text "Thank you for rating" when someone rate
    $starBar .= '</div>';

    return $starBar;
}

echo starBar(5, 103, 16); // We create star bar     
?>

的tuto星级rating.js

function rateMedia(mediaId, rate, numStar) {
    $('.box' + mediaId).html('<img src="comment/loader-small.gif" alt="" />'); // Display a processing icon
    var data = {mediaId: mediaId, rate: rate}; // Create JSON which will be send via Ajax

    $.ajax({ // JQuery Ajax
        type: 'POST',
        url: 'comment/tuto-star-rating.php', // URL to the PHP file which will insert new value in the database
        data: data, // We send the data string
        dataType: 'json',
        timeout: 3000,
        success: function(data) {
            $('.box' + mediaId).html('<div style="font-size: small; color: green">Thank you for rating</div>'); // Return "Thank you for rating"
            // We update the rating score and number of rates
            $('.resultMedia' + mediaId).html('<div style="font-size: small; color: grey">Rating: ' + data.avg + '/' + numStar + ' (' + data.nbrRate + ' votes)</div>');

            // We recalculate the star bar with new selected stars and unselected stars
            var ratingBar = '';
            for ( var i = 1; i <= numStar; i++ ) {
                ratingBar += '<div class="';
                if (i <= data.avgCeil) ratingBar += 'star_selected'; else ratingBar += 'star';
                ratingBar += '"></div>';
            }

            $('#' + mediaId + ' .star_bar').html(ratingBar).off('mouseenter');
        },
        error: function() {
            $('#box').text('Problem');
        }
    });
}

$(function () {
    $('.star_bar').on('mouseenter', function overBar(event) { // Mouse enter the star bar
        var relData = $.parseJSON($(this).attr('rel')); // Get JSON values: number of stars and media ID

        $(this).css('cursor','pointer');

        // We create a new star bar OVER the previous one with transparent stars
        var newStarBar = '';
        for ( var i = 1; i <= relData.numStar; i++ ) {
            newStarBar += '<div class="no_star" id="' + i + '" title="' + i + '/' + relData.numStar + '" onclick="rateMedia(' + relData.mediaId + ', ' + i + ', ' + relData.numStar + '); return false;"></div>';
        }
        $(this).css('position', 'relative').append('<div id="over' + relData.mediaId + '" style="position:absolute; top:0; left:0;">' + newStarBar + '</div>');

        // When we move the mouse over the new transparent star bar they become blue
        $('#over' + relData.mediaId + ' > div').mouseover(function() {
            var myRate = $(this).attr('id');
            for ( var i = 1; i <= relData.numStar; i++ ) {
                if (i <= myRate) $('#over' + relData.mediaId + ' #' + i).attr('class', 'star_hover');
                else $('#over' + relData.mediaId + ' #' + i).attr('class', 'no_star');
            }
        });
    });

    // Mouse leaves the star bar, we remove the rating bar
    $('.star_bar').on('mouseleave', function overBar(event) {
        var relData = $.parseJSON($(this).attr('rel'));
        $('#over' + relData.mediaId).remove();
    });
});

0 个答案:

没有答案