好吧所以我想用php和javascript制作一个5星评级系统。我一直在关注本教程http://www.youtube.com/watch?v=uxY6PrIxdiA。数据库部分工作,我可以进入数据库并键入值,它将添加所有,并告诉我平均评级和有多少人评级。我只是在让按钮工作时遇到问题。 索引代码:
<?php include("avg.php"); ?>
<head>
<script type="text/javascript">
function ratings (elem) {
var x = new XMLHttpRequest ();
var url = "rateParse.php";
var a = document.getElementById(elem).value;
var vars = "choice="+a;
x.open("POST", url, true);
x.setRequestHandler("Content-type", "application/x- www-form-urlencoded");
x.onreadystatechange = function() {
if(x.readyState == 4 && x.status ==200) {
var return_data = x.responseText;
document.getElementById("status").innerHTML = return_data;
}
}
x.send(vars); //execute the request
document.getElementById("status").innerHTML = "processing...";
}
</script>
</head>
<body>
<div id="ratings">
<p> Rate this item</p>
<input type="button" value="1" onclick="ratings('1');">
<input type="hidden" name="choice" id="1" value="1">
<input type="button" value="2" onclick="ratings('2');">
<input type="hidden" name="choice" id="2" value="2">
<input type="button" value="3" onclick="ratings('3');">
<input type="hidden" name="choice" id="3" value="3">
<input type="button" value="4" onclick="ratings('4');">
<input type="hidden" name="choice" id="4" value="4">
<input type="button" value="5" onclick="ratings('5');">
<input type="hidden" name="choice" id="5" value="5">
<p><?php echo $rating; ?></p>
<div id="status"></div>
</div>
</body>
</html>
avg:
<?php
include_once("scripts/connect_db.php");
$sql = mysql_query("SELECT ratings FROM blog_posts WHERE id='1'");
while($row = mysql_fetch_array($sql)){
$myNums = $row["ratings"];
$kaboom = explode(",", $myNums);
$count = count($kaboom);
$sum = array_sum($kaboom);
$avg = $sum / $count;
$roundit = floor($avg);
if($roundit == 0) {
$rating = "This ... has not yet been rated. You can be first!";
}else if ($count == 1) {
$rating = "Average rating for the ... is currently $roundit<br /> This ... has been rated by $count person.";
}else if($count > 1) {
$rating = " Average rating for the ... is currently $roundit<br /> This ... has been rated by $count people.";
}else{
$rating = "sorry there is an error in the system... please try refreshing the page";
}
}
?>
rateParse
<?php
if(isset($_POST["choice"])){
$choice = preg_replace('#[^0-9]#i' '', $_POST['choice']);
if($choice > 5) {
echo "stop playing around";
exit();
}else if($choice < 1) {
echo "stop playing around";
exit();
}else{
$ipaddress = getenv('REMOTE_ADDR');
include_once("scripts/connect_db.php");
$sql_check = mysql_query("SELECT * FROM rating_ip WHERE a_id='1' AND ipaddress='$ipaddress' LIMIT 1");
$num_rows = mysql_num_rows($sql_check);
if($num_rows > 0) {
echo '<p>Sorry, you have already rated this</p>';
exit();
}
$sql = mysql_query("SELECT ratings FROM blog_posts WHERE id='1'"); //change blog_posts to my own table
while($row = mysql_fetch_array($sql)){
$myNums = $row["ratings"];
$kaboom = explode(",", $myNums);
array_push($kaboom, $choice);
$string = implode(",", $kaboom);
$firstChar = substr($string, 0, 1);
$lastChar = substr($string, strlen($string) -1, 1);
if($firstChar == ","){
$string = $choice;
}
if($lasChar == ","){
$string = substr($string, strlen($string) -1, 1);
}
$update = mysql_query("UPDATE blog_posts SET ratings='$string' WHERE id='1'"); //change blog_posts to my own table
$insert = mysql_query("INSERT INTO rating_ip (a_id, ipaddress) VALUES ('1', '$ipaddress')")or die(mysql_error());
echo '<p> Thankyou! You have given this ... a rating of ' . $choice . '</p>';
exit();
}
}
}
?>