简体   繁体   English

简单的 ajax 星级与 php

[英]Simple ajax star rating with php

I trying to setup a simple 5 star rating system and have managed to thoroughly frustrate myself.我试图设置一个简单的 5 星评级系统,并成功地彻底挫败了自己。 I am already using one PHP query pull from the table called "place".我已经在使用一个来自名为“place”的表的 PHP 查询拉取。 I have a second table labeled "vote".我有第二张标有“投票”的表格。 I have no problems with the "place" table, those elements populate just fine.我对“放置”表没有任何问题,这些元素填充得很好。 The only issues is getting the Ajax requests to work with the PHP which at this time is leaving sobbing in a corner.唯一的问题是让 Ajax 请求与 PHP 一起工作,此时它正在角落里抽泣。

I been trying to use the following tutorial but for all my effort can't seem to figure out what is blocking my code from executing properly?我一直在尝试使用以下教程,但我似乎无法弄清楚是什么阻止了我的代码正常执行?

http://sandbox.ronggur.com/2010/01/19/jquery-tutorial-simple-ajax-star-rating-with-php-extended/ http://sandbox.ronggur.com/2010/01/19/jquery-tutorial-simple-ajax-star-rating-with-php-extended/

Any help is greatly appreciated.任何帮助是极大的赞赏。

Here's my Code这是我的代码

    <div id="pagewrap">
        <div id="widgets">
<?php

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);



   $query = "SELECT * FROM place";

        $data = mysqli_query($dbc, $query)
            or die("Error: ".mysqli_error($dbc));   

            while ($row = mysqli_fetch_array($data)) 
            {       
                echo '<div class="content">';                                           
                    echo '<p id="type"> ' . $row['free'] . $row['paid'] . '</p>';  
                    echo '<div id="holder">';

                    echo '<div id="loc_cont"><a href="site_info.php?id=' . $row['placeId'] . '"><img id="place_logo" alt="' . $row['placename'] . '" src="' . THUMBNAILS . $row['thumb'] . '" /></a>';

                        echo '</div>';

                        echo '<div class="info">';

                        echo '<p id="loc">' . $row['placename'] . ' - ' . $row['city'] . '</p>';

                        echo '</div>';
                   echo ' </div>';
                    echo '<div class="review">';
                    echo '<div class="rates">';
                    include_once('PHP/rating.php');
                        #$star = fetchStar();
                        #echo '<h2>Star Rater - '. $row['placeId'] .'</h2>';
                        echo '<ul class="star-rating" id="star-rating-<'. $row['placeId'] .'"';
                            echo '<li current-rating-<'. $row['placeId'] .'" style="width:getRating('. $row['placeId'] .')%"><!-- will show current rating --></li>';
                                echo '<span id="'. $row['placeId'] .'">';
                                    echo '<li><a href="javascript:void(0)" title="1 star out of 5" class="one-star">1</a></li>';
                                    echo '<li><a href="javascript:void(0)" title="2 stars out of 5" class="two-stars">2</a></li>';
                                    echo '<li><a href="javascript:void(0)" title="3 stars out of 5" class="three-stars">3</a></li>';
                                    echo '<li><a href="javascript:void(0)" title="4 stars out of 5" class="four-stars">4</a></li>';
                                    echo '<li><a href="javascript:void(0)" title="5 stars out of 5" class="five-stars">5</a></li>';
                                echo '</span>';
                        echo '</ul>';
                    echo '</div>';
                    echo '</div>';
               echo '</div>';
          }                  

mysqli_close($dbc);

?>
        </div>
</div><!--End PageWrap div-->
    <?php
    if($_GET['do']=='rate'){
         // do rate and get id
         rate($_GET['placeId']);
            }else if($_GET['do']=='getrate'){
             // get rating and get id
             getRating($_GET['placeId']);
            }

    // function to retrieve
    function getRating($placeId){
      $sql= "select * from vote where placeId = '".$placeId."' ";
     $result=@mysql_query($sql);
     $rs=@mysql_fetch_array($result);
     // set width of star
     $rating = (@round($rs[value] / $rs[counter],1)) * 20;
     echo $rating;
    }


    // function to insert rating
    function rate($placeId){
     $text = strip_tags($_GET['rating']);
      $update = "UPDATE vote SET counter = counter + 1, value = value + ".$_GET['rating']."  WHERE placeId = '".$placeId."' ";

     $result = @mysql_query($update);
    }
    ?>

/




 / JavaScript Document
     $(document).ready(function() {
     // get rating function


     function getRating(id){
     $.ajax({
     type: "GET",
     url: "../PHP/rating.php",
     data: "do=getrate&placeId="+id,
     cache: false,
     async: false,
     success: function(result) {
     // apply star rating to element
     $("#current-rating-"+id+"").css({ width: "" + result + "%" });
       },
     error: function(result) {
     alert("some error occured, please try again later");
     }
     });
     }

     // link handler
     $('.rates li a').click(function(){
     // get the parent id
     var idStar = $(this).parent().parent().attr('id');
     $.ajax({
     type: "GET",
     url: "../PHP/rating.php",
     data: "rating="+$(this).text()+"&do=rate&placeId="+idStar,
     cache: false,
     async: false,
     success: function(result) {
     // remove #ratelinks element to prevent another rate
     $("#ratelinks").remove();
     // get rating after click
     getRating(idStar);
     },
     error: function(result) {
     alert("some error occured, please try again later");
     }
     });

     });
     });

Nathan, I dont have coffee with me right now so cant go through your code. Nathan,我现在没有和我一起喝咖啡,所以不能通过你的代码拨打 go。 But can direct you to link where something similar is available with code hints.但是可以通过代码提示指导您链接到类似内容可用的地方。

Click Here 点击这里

You can try this http://orkans-tmp.22web.net/star_rating/index.html its very easy to implement.你可以试试这个http://orkans-tmp.22web.net/star_rating/index.html它很容易实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM