編輯:有問題的插件位於here。JQuery和PHP星級簡化
PHP初學者在這裏使用JQuery星級評分片段,並得到它完美的工作。我的問題是,它目前配置爲計數並顯示多個評級的平均值(針對公共應用程序)。我試圖簡化插件,以便它可以設置個人評分(就像在iTunes中爲自己的歌曲評分一樣)。用戶可以更新他們的評分,但是不會有部分明星存在。我已經打破了插件多次試圖讓它工作,但無濟於事。 MySQL數據庫存在如下:
CREATE TABLE IF NOT EXISTS `pd_total_vote` (
`id` int(11) NOT NULL auto_increment,
`desc` varchar(50) NOT NULL,
`counter` int(8) NOT NULL default '0',
`value` int(8) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
如果我能得到它的工作我想象的樣子,我也不會要求雙方counter
和value
列,只是包含1和5之間的數值單INT列。目前counter
累計得票數,而value
累計收視率。然後使用(值/計數器)* 20(以百分比)顯示星星。在PHP低於(原件):
<?php
// connect to database
$dbh=mysql_connect ("localhost", "user", "pass") or die ('Cannot connect to the database');
mysql_select_db ("thenally_pd",$dbh);
if($_GET['do']=='rate'){
rate($_GET['id']);
}else if($_GET['do']=='getrate'){
// get rating
getRating($_GET['id']);
}
// get data from table
function fetchStar(){
$sql = "select * from `pd_total_vote`";
[email protected]_query($sql);
while($rs = @mysql_fetch_array($result,MYSQL_ASSOC)){
$arr_data[] = $rs;
}
return $arr_data;
}
// function to retrieve
function getRating($id){
$sql= "select * from `pd_total_vote` where id='".$id."' ";
[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 set rating
function rate($id){
$text = strip_tags($_GET['rating']);
$update = "update `pd_total_vote` set counter = counter + 1, value = value + ".$_GET['rating']." where id='".$id."' ";
$result = @mysql_query($update);
}
?>
感謝在正確的方向上來看,
邁克
這是一個好主意。我會盡力,儘快回覆你。感謝您的意見。我編輯了OP來顯示該插件的鏈接。 – TheNally 2012-04-02 20:56:21
我發現了!謝謝,@Kyra。設置計數器= 1仍然允許值增加,產生偏斜的結果。結束了工作的是在最後的「功能設置評分」中更改$ update。工作行如下所示:'$ update =「UPDATE'pd_total_vote'SET counter = 1,value =」。$ _ GET ['rating']。「WHERE id ='」。$ id。「'」;'Counter always等於1,並使其每個新值都不會添加到前面的值中。我意識到這仍然是一個半解決方案,但它現在會做。再次感謝。 – TheNally 2012-04-02 21:22:13
這真棒,它的工作。 :) – Kyra 2012-04-02 21:58:45