我一直在嘗試爲我的腳本全天添加一些新功能,例如,我希望能夠顯示我的星級評分圖像下的平均速率和投票數,例如某些東西在下面的例子中。顯示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>
你不應該使用@運算符(或者至少,只有在嚴格必要的情況下);;看到http://derickrethans.nl/five-reasons-why-the-shutop-operator-should-be-avoided.html有幾個原因 - 最重要的是它會阻止你看到任何錯誤可能會發生(這意味着它將更難調試) –
那麼當你運行它會發生什麼? – czarchaic
你也不應該使用'async:false' *(除了幾個非常特殊的情況)*。 – nickf