2010-01-06 38 views
0

我一直在嘗試爲我的腳本全天添加一些新功能,例如,我希望能夠顯示我的星級評分圖像下的平均速率和投票數,例如某些東西在下面的例子中。顯示PHP函數的JQuery

********** 
9.8/10 (1160 vote cast) 

我一直在試一切,遇到問題後遇到新問題。例如,現在每個新的PHP函數都會互相干擾。

例如我的第一個PHP函數getRating()本身都正常工作,但它不會正確地與我的第二個PHP函數evaluateRatingText()和我的第一個PHP函數使用時,它顯示通過顯示的平均得分都錯了打亂了我的第二個PHP函數例如它實際顯示值兩次。

那麼我該如何讓我的PHP函數和我的JQuery函數一起正常工作,它能夠正確顯示我的第一個php函數getRating(),以便我的JQuery函數可以相應地顯示這兩個PHP函數?任何人都可以幫助我,例子會很棒嗎?

這是我的PHP函數。

// function to retrieve 
function getRating(){ 
    $sql= "select * from vote"; 
    [email protected]_query($sql); 
    [email protected]_fetch_array($result); 
    // set width of star 
    $rating = (@round($rs['value']/$rs['counter'],1)) * 10; 
    echo $rating; 
} 

// function to retrieve average rating and votes 
function evaluateRatingText(){ 
    $sql= "select * from vote"; 
    [email protected]_query($sql); 
    [email protected]_fetch_array($result); 
    $avg = (@round($rs['value']/$rs['counter'],1)) * 10; 
    $votes = $rs['counter']; 
    echo $avg . "/10 (" . $votes . " votes cast)"; 
} 

這裏是JQuery函數。

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

HTML標記。

<ul class='star-rating'> 
    <li class="current-rating" id="current-rating"></li> 
    <li class="rating-text" id="rating-text"></li> 
    <span id="ratelinks"> 
    <li><a href="javascript:void(0)" title="1 star out of 10" class="one-star">1</a></li> 
    <li><a href="javascript:void(0)" title="2 stars out of 10" class="two-stars">2</a></li> 
    <li><a href="javascript:void(0)" title="3 stars out of 10" class="three-stars">3</a></li> 
    <li><a href="javascript:void(0)" title="4 stars out of 10" class="four-stars">4</a></li> 
    <li><a href="javascript:void(0)" title="5 stars out of 10" class="five-stars">5</a></li> 
    <li><a href="javascript:void(0)" title="6 stars out of 10" class="six-star">6</a></li> 
    <li><a href="javascript:void(0)" title="7 stars out of 10" class="seven-stars">7</a></li> 
    <li><a href="javascript:void(0)" title="8 stars out of 10" class="eight-stars">8</a></li> 
    <li><a href="javascript:void(0)" title="9 stars out of 10" class="nine-stars">9</a></li> 
    <li><a href="javascript:void(0)" title="10 stars out of 10" class="ten-stars">10</a></li> 
    </span> 
</ul> 
+0

你不應該使用@運算符(或者至少,只有在嚴格必要的情況下);;看到http://derickrethans.nl/five-reasons-why-the-shutop-operator-should-be-avoided.html有幾個原因 - 最重要的是它會阻止你看到任何錯誤可能會發生(這意味着它將更難調試) –

+0

那麼當你運行它會發生什麼? – czarchaic

+0

你也不應該使用'async:false' *(除了幾個非常特殊的情況)*。 – nickf

回答

1

如果您使用

echo $rating; 

打印的評價是10分,那麼就呼應使用該等級和投票數的字符串:

echo $rating . "/10 (" . $votesCast . " votes cast)"; 

看起來你幾乎在那裏。

+0

仍然無法讓它工作:) – Jawa

0

添加這個...

<ul class='star-rating'> 
    <li class="current-rating" id="current-rating"></li> 
    <li class="rating-text" id="rating-text"></li> 
    <span id="ratelinks"> 
    [...] 
    </span> 
</ul> 

這...

function getRating(){ 
    $.ajax({ 
     [...] 
     success: function(result) { 
      // apply star rating to element 
      $("#current-rating").css({ width: "" + result + "%" }); 
      // add rating text 
      $("#rating-text").text(evaluateRatingText()); 
     }, 
     [...] 
    }); 
} 

function evaluateRatingText() { 
    // Replace the line below with a new call to $.ajax() that gets the information you need from a server-side resource; sorry, I don't know PHP 
    return "9.8/10 (1160 vote cast)"; 
} 
1

其中一個問題是在這條線,在JavaScript success回調:

$("#rating-text").text(evaluateRatingText()); 

它看起來像你希望它會調用PHP函數,它不會。它實際上再次調用相同的JavaScript函數(發送另一個請求)。

你可能想把它改成這樣:

$("#rating-text").text(result); 
+0

這有點固定它,但現在它搞砸了我的星級評價圖像。並顯示我的平均評分5555/10任何解決方案如何解決這個問題?不過謝謝。 – Jawa

3

你的PHP函數不檢索數據,他們立即將其打印出來。

而不是'回聲'你應該使用'返回'。

它顯示評分加倍的原因是這兩個函數都回應平均評分,只有evaluateRatingText會附加更多數據。

也許你會更好的開始與一些基本的PHP教程有關函數,變量和處理返回值之前混合PHP和JavaScript。

0
從HTML問題

除了(橫跨包裝的利和文字列表的奇怪的混合體),你應該做一個回報功能加上計算函數來處理這樣的

PHP整件事

function echoRatingAll() 
{ 
    list($value, $counter) = getRating2(); //function returns doesn't print 
    echo "$value,$counter"; //we need to echo for the AJAX & we comma delimit 

} 
function getRating2(){ 
    $sql= "select `value`, `counter` from vote"; 
    [email protected]_query($sql); 
    list($v,$c) = mysql_fetch_row($result); 

    return array($v, $c); 
} 

JS

success: function(result) { 
      var values=result.split(",", result); 
      var value = values[0]; 
      var counter = values[1]; 
      //do your jquery stuff from here with the two values 
     } 

,如果你不知道如何做在Javascript中平均只讓PHP返回3個值,而不是2