前兩篇談的是純視覺層的星級呈現,這一篇要把它變成真的能投票、能算平均、能擋重複投票的功能。
只要能做到三件事,星級評分就成立了:攔下 onclick、把這一票寫進資料庫、再從資料庫把總票數與總分讀出來相除,就是目前的平均星數。
以下是引用片段:
CREATE TABLE ratings(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
total_votes INT NOT NULL,
total_value INT NOT NULL,
which_id INT NOT NULL,
which_table VARCHAR(255),
used_ips LONGTEXT NULL
);
total_votes 記票數、total_value 記分數總和、used_ips 用序列化字串存下已投票的 IP,防重複就靠它。
以下是引用片段:
<?php
require("connectDB.php");
require("closeDB.php");
require("tableName.php");
require("openDB.php");
?>
以下是引用片段:
<?php
$rating_posted=$_GET['vote'];//由星星傳回的分值
$id=$_GET['id'];
$query=mysql_query("SELECT total_votes, total_value, used_ips FROM $tableName WHERE id='".$id."' ")or die(" Error: ".mysql_error());
$numbers=mysql_fetch_assoc($query);
$checkIP=unserialize($numbers['used_ips']);
$count=$numbers['total_votes'];//總票數
$current_rating=$numbers['total_value'];//分數總和
$sum=$rating_posted+$current_rating;//加上這一票
$tense=($count==1) ? "vote" : "votes";
$voted=@mysql_fetch_assoc(@mysql_query("SELECT title FROM $tableName WHERE used_ips LIKE '%".$_SERVER['REMOTE_ADDR']."%' AND id='$id' "));//比對IP,回傳true或false
if($voted){
echo "<div class=/"rating/">".
"<ul class=/"star-rating/">".
"<li class=/"current-rating/" style=/"width:". @number_format($current_rating/$count,2)*20 ."px;/">Current rating.</li>".
"<li class=/"one-star/">1</li>".
"<li class=/"two-stars/" >2</li>".
"<li class=/"three-stars/">3</li>".
"<li class=/"four-stars/">4</li>".
"<li class=/"five-stars/">5</li>".
"</ul>".
"<p>Rating: <strong>".@number_format($current_rating/$count,2)."</strong> {".$count." ".$tense." cast} <br />You have previously voted.</p></div>";//已投票者只看結果
}else{
if(isset($_GET['vote'])){
if($sum==0){
$added=0;//處理第一票
}else{
$added=$count+1;//票數加一
}
if(is_array($checkIP)){
array_push($checkIP,$_SERVER['REMOTE_ADDR']);//已有紀錄則追加
}else{
$checkIP=array($_SERVER['REMOTE_ADDR']);//第一次投票
}
$insert=serialize($checkIP);
mysql_query("UPDATE $tableName SET total_votes='".$added."', total_value='".$sum."', used_ips='".$insert."' WHERE id='".$_GET['id']."'");
echo "<div class=/"rating/"><p>Rating: <strong>".@number_format($sum/$added,2)."</strong> {".$added." ".$tense." cast} <span>Thank you for your vote!</span></p></div>";//顯示更新後的結果
}else{
?>
以下是引用片段:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>CSS: Star Rater Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="styles2-1.css" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<div class="rating">
<p>How clear was this tutorial?</p>
<ul class="star-rating">
<li class="current-rating" style="width:<?php echo @number_format($current_rating/$count,2)*20 ?>px;">Current rating</li>
<li><a href="<?php echo $_SERVER['PHP_SELF'] . "?" . $_GET['section'] . "&id=" . $_GET['id'] . "&vote=1";?>" title="Rate this 1 star out of 5" class="one-star">1</a></li>
<li><a href="<?php echo $_SERVER['PHP_SELF'] . "?" . $_GET['section'] . "&id=" . $_GET['id'] . "&vote=1";?>" title="Rate this 2 stars out of 5" class="two-stars" >2</a></li>
<li><a href="<?php echo $_SERVER['PHP_SELF'] . "?" . $_GET['section'] . "&id=" . $_GET['id'] . "&vote=1";?>" title="Rate this 3 stars out of 5" class="three-stars" >3</a></li>
<li><a href="<?php echo $_SERVER['PHP_SELF'] . "?" . $_GET['section'] . "&id=" . $_GET['id'] . "&vote=1";?>" title="Rate this 4 stars out of 5" class="four-stars" >4</a></li>
<li><a href="<?php echo $_SERVER['PHP_SELF'] . "?" . $_GET['section'] . "&id=" . $_GET['id'] . "&vote=1";?>" title="Rate this 5 stars out of 5" class="five-stars" >5</a></li>
</ul>
</body></html>
以下是引用片段:
<?php
echo "<p>Rating: <strong>".@number_format($sum/$count,2)."</strong> {".$count." ".$tense." cast}</p></div>";
} // end isset get vote
} //end voted true, false
?>
把結果落庫的部分留到下一篇再細寫。*20 這個換算是關鍵:一顆星 20px,五顆星剛好鋪滿 100px 寬的容器。
原文:Creating a star rating system part (3)
鏈接:https://slim.climaxdesigns.com/tutorial.php?id=3
版權:版權歸原作者所有,翻譯文檔版權歸本人|greengnn
我們用 PHP 實現,效果可參考:https://gnn.80x86.cn/starrating
補一句現實提醒:這份範例的 SQL 直接拼接 $_GET 參數,用 IP 比對防重複也很容易被繞過。若要沿用,請改用 prepared statement,並把防重複換成 cookie 或登入身分驗證。