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.