Jump to content


Photo

Rate images


  • Please log in to reply
2 replies to this topic

#1 DeathRay2K

DeathRay2K
  • Members
  • 96 posts
  • Location:Canada
  • Projects:D2K Studios, Xaeon Mod, Unseen Presence, Rhythm Mixer, Website Designs, CSS OS

Posted 04 December 2005 - 03:50 AM

Yes, I have yet another question for ye masters of PHP.
Now I'm trying to make a rating system, and all was going perfectly, until one problem occurred.
When I try to add all the ratings together, for instance 5+5+5, it gives me them one after another, as in 555. I'm really not sure why this is, as I made sure that the added number is an integer...
Well, here's the code, I'm sure there are horrible flaws in it, because as I've said previously, I'm new to this...
 <?php
 	 $ratingfile = 'rating.txt';

 	 if(isset($_POST['rate'])){
    if(count(explode('.', $rated)) >=2){
   	 $add = '.' . $_POST['rate'];
    }
    else{
   	 $add = $_POST['rate'];
    }
    $rfha = fopen($ratingfile, 'a');
    $ratings = fwrite($rfha, $add);
    fclose($rfha);
 	 }
	

 	 if(file_exists($ratingfile)){
    $addrates = 0;
    $rfhr = fopen($ratingfile, 'r');
    $ratings = fread($rfhr, filesize($ratingfile));
    fclose($rfhr);
    $rates = explode('.', $ratings);
    
    $numratings = count($rates);
    
    for($ratearray = 0; $ratearray < $numratings; $ratearray += 1){
   	 settype($rates[$ratearray], 'int');
   	 $addrates += $rates[$ratearray];
   	 echo gettype($rates[$ratearray]) . '<br>' . $ratearray . '<br>';
   	 $ratearray = $ratearray + 1;
    }
    $rating = $addrates/$numratings;
 	 }
 	 else{
    $rating = 'Unrated';
 	 }

 	 echo $rating . '<br>' . $addrates . '<br>' . $numratings;
  ?>
  <form action="test.php" method="post" target=_self>
 	 <select name="rate">
    <option disabled="disabled">Best</option>
    <option value="5">5</option>
    <option value="4">4</option>
    <option value="3">3</option>
    <option value="2">2</option>
    <option value="1">1</option>
    <option disabled="disabled">Worst</option>
 	 </select>
 	 <button type="submit">Submit</button>
  </form>

All of this code is in a test.php, which you can check out here.

Edited by DeathRay2K, 04 December 2005 - 03:51 AM.


#2 Mastermind

Mastermind

    Server Technician

  • Undead
  • 7,014 posts
  • Location:Cambridge, MA
  • Projects:MasterNews 3
  •  The Man Behind the Curtain

Posted 04 December 2005 - 04:56 AM

floatval($var) would probably be useful. What you're getting is concatenation because your values are strings, not numbers.
Posted Image

Well, when it comes to writing an expository essay about counter-insurgent tactics, I'm of the old school. First you tell them how you're going to kill them. Then you kill them. Then you tell them how you just killed them.

Too cute! | Server Status: If you can read this, it's up |

#3 DeathRay2K

DeathRay2K
  • Members
  • 96 posts
  • Location:Canada
  • Projects:D2K Studios, Xaeon Mod, Unseen Presence, Rhythm Mixer, Website Designs, CSS OS

Posted 04 December 2005 - 05:17 AM

floatval($val) won't help, it can't be used on arrays...
Besides, I used settype to convert the string into an integer. It still happens when I convert the string into a float.

Now I've tried with floatval($val) too, and the problem still occurs. Here's a snippet of the code.
   for($ratearray = 0; $ratearray < $numratings; $ratearray += 1){
   	 $temprates = $rates[$ratearray];
   	 $addrates += floatval($temprates);
   	 echo gettype($rates[$ratearray]) . '<br>' . $ratearray . '<br>';
    }
    $rating = $addrates/$numratings;

Alright, I've been playing around, and the cause is not the things added being strings, the problem is that its not putting '.'s between the comments in the file, as it should. This is the code that's not working:
 if(isset($_POST['rate'])){
    if(file_exists($ratingfile)){
   	 $rfho = fopen($ratingfile, 'r');
   	 $rated = fread($rfho, filesize($ratingfile));
   	 fclose($rfho);
    }
    else{
   	 $rated = '0';
    }
    if($rated == '.'){
   	 $add = '.' . $_POST['rate'];
   	 echo 'Added';
    }
    else{
   	 $add = $_POST['rate'];
   	 echo 'Not Added';
    }
    $rfha = fopen($ratingfile, 'a');
    $ratings = fwrite($rfha, $add);
    fclose($rfha);
 	 }
I also tried checking for '.'s with explode('.', $rated), but that doesn't work either...


EDIT:
Huzzah! Success!
I used file($ratingsfile) instead of the whole fopen, fread, fclose junk. :sad:

Edited by DeathRay2K, 04 December 2005 - 06:06 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users