2012-04-11 122 views
0

我试图设置一个简单的5星评级系统,并设法彻底挫败自己。我已经在名为“place”的表中使用了一个PHP查询拉。我有第二张标有“投票”的表格。我对“地点”表没有任何问题,这些元素填充得很好。唯一的问题是让Ajax请求与PHP一起工作,而PHP现在正在一个角落里哭泣。简单的ajax星级评定与php

我一直在尝试使用下面的教程,但对于我所有的努力似乎无法弄清楚什么阻止我的代码正确执行?

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

任何帮助是极大的赞赏。

这里是我的代码

<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."' "; 
    [email protected]_query($sql); 
    [email protected]_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"); 
    } 
    }); 

    }); 
    }); 

回答

0

弥敦道, 我没有跟我的咖啡,现在这样不能去通过您的代码。但是可以指导你链接哪些类似的代码提示。

Click Here

+0

我当然很欣赏这个资源的快速反应和感谢。我相信这个问题介于PHP和JavaScript getRating函数之间。鉴于我对JavaScript的使用经验,我曾希望无论发生什么问题都会破坏我的代码,这可能是我没有看到的。我真的很感激,如果任何人可以腾出一些时间来审查,看看有什么可能导致休息。 – 2012-04-11 07:13:22