如何显示当前评级?
我想知道是否有人可以为我发布我需要对此代码进行的更改,以便在提交表单之前和之后在页面上显示当前的评级值,就像现在编写的那样。谢谢。
<?php echo '<html>
<head>
<title>Rating Tool Test</title>
</head>
<body>';
if ( (!isset($_POST['submit'])) ) {
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">
Your Rating: <select name="rate">';
for ($i = 1; $i <= 5; $i++) {
echo "<option value=\"$i\">$i</option>"; }
echo '</select><br /><input type="submit" value="Rate it!" name="submit"/>
</form>';
}
else {
$rate = isset ($_POST['rate']) ? $_POST['rate'] : 0;
$filename = "ratings";
$alreadyRated = false;
$totalRates = 0;
$totalPoints = 0;
$ip = getenv('REMOTE_ADDR');
$oldResults = file('results/'.$filename.'.txt');
foreach ($oldResults as $value) {
$oneRate = explode(':',$value);
if ($ip == $oneRate[0]) $alreadyRated = true;
$totalRates++;
$totalPoints += $oneRate[1];
}
if ((!$alreadyRated) && ($rate > 0)){
$f = fopen('results/'.$filename.".txt","a+");
fwrite($f,$ip.':'.$rate."\n");
fclose($f);
$totalRates++;
$totalPoints+=$rate;
}
echo 'Total Average Rating:<br />'.substr(($totalPoints/$totalRates),0,3).' Out Of 5.<br />Total Votes: '.$totalRates.'<br />';
for ($i=0;$i<round(($totalPoints/$totalRates),0);$i++){
echo '<img src="style/star.gif" alt="star" />';
}
echo '</body>
</html>';
}
?>
I was wondering if someone could post for me the changes I need to make to this code to get the current rating values to display on the page before the form is submitted as well as after, as it is written now. Thanks.
<?php echo '<html>
<head>
<title>Rating Tool Test</title>
</head>
<body>';
if ( (!isset($_POST['submit'])) ) {
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">
Your Rating: <select name="rate">';
for ($i = 1; $i <= 5; $i++) {
echo "<option value=\"$i\">$i</option>"; }
echo '</select><br /><input type="submit" value="Rate it!" name="submit"/>
</form>';
}
else {
$rate = isset ($_POST['rate']) ? $_POST['rate'] : 0;
$filename = "ratings";
$alreadyRated = false;
$totalRates = 0;
$totalPoints = 0;
$ip = getenv('REMOTE_ADDR');
$oldResults = file('results/'.$filename.'.txt');
foreach ($oldResults as $value) {
$oneRate = explode(':',$value);
if ($ip == $oneRate[0]) $alreadyRated = true;
$totalRates++;
$totalPoints += $oneRate[1];
}
if ((!$alreadyRated) && ($rate > 0)){
$f = fopen('results/'.$filename.".txt","a+");
fwrite($f,$ip.':'.$rate."\n");
fclose($f);
$totalRates++;
$totalPoints+=$rate;
}
echo 'Total Average Rating:<br />'.substr(($totalPoints/$totalRates),0,3).' Out Of 5.<br />Total Votes: '.$totalRates.'<br />';
for ($i=0;$i<round(($totalPoints/$totalRates),0);$i++){
echo '<img src="style/star.gif" alt="star" />';
}
echo '</body>
</html>';
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是您要求的更改:
Here are the changes you requested: