如何显示当前评级?

发布于 2024-10-15 00:07:34 字数 1461 浏览 9 评论 0原文

我想知道是否有人可以为我发布我需要对此代码进行的更改,以便在提交表单之前和之后在页面上显示当前的评级值,就像现在编写的那样。谢谢。

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戴着白色围巾的女孩 2024-10-22 00:07:34

以下是您要求的更改:

<?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>';
}
$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>';
?>

Here are the changes you requested:

<?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>';
}
$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>';
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文