PHP Script Help Please

Hi guys

I need some help. I am creating a rating script for my website and want multiple rating facility on one page. Below is the PHP I have come up with but it’s not working. The “filename” bit ($filename) I change each occurance of the script so that, in theory, the results all store in different files. However, when rate is clicked on it saves the same thing to every result file - meaning visitors will only be able to vote once and then the same vote is added to everything. Any ideas?

<?php if ( (!isset($_POST['submit'])) ) { ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <table width="100%">
          <tr><td>Your rating:</td><td><select name="rate">
          <?php for ($i = 1; $i <= 10; $i++) { echo "<option value="$i">$i</option>"; } ?>
          <tr><td colspan="2" align="center"><input type="submit" value="Rate it!" name="submit"/></td></tr>
   <?php } 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;	   
        	$totalPoints += $oneRate[1];

        if ((!$alreadyRated) && ($rate > 0)){            
           $f = fopen('results/'.$filename.".txt","a+");         


<?php echo "Actual rating from $totalRates rates is: ".substr(($totalPoints/$totalRates),0,3)."
"; for ($i=0;$i<round(($totalPoints/$totalRates),0);$i++){ echo "s"; } echo "
"; } ?>
$filename = "ratings";
$oldResults = file('results/'.$filename.'.txt');
$f = fopen('results/'.$filename.".txt","a+"); 

The filename doesn’t get changed at all. I’m not sure if you’re doing this manually with each occurrance, but I think it would be in your best interest, no matter what, that you start looking in the direction of a database, rather than using remote files.

I am doing it manually - so each time I put the rating script on the page I change the filename :)

I am using the flat files rather than a database as there will be a very small number of voters - it’s for some students I teach - and will be in a password protected area too of the website. At most there will be about 20 votes for any one item.

Sponsor our Newsletter | Privacy Policy | Terms of Service