开发者

How Can I Display The Current Rating? [closed]

开发者 https://www.devze.com 2023-02-07 03:29 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

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>';
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消