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 s开发者_Python百科rc="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>';
?>
精彩评论