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

1 回答 1

1

以下是您要求的更改:

<?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>';
?>
于 2011-01-29T00:08:38.703 回答