blueman378 Posted December 27, 2007 Share Posted December 27, 2007 hi guys im making a simple rating system and i need a way to work out the percentage, when a user votes get what i mean? and so the script will be something like <?php //Retrieve Vote $vote = $_GET['vote']; //Retrieve current value $game = $_GET['game']; global $database; $q = "SELECT * FROM " . Games . " WHERE gName = '$game' ORDER BY `gName` ASC "; $result = $database->query($q) or die("Error: " . mysql_error()); /* Error occurred, return given name by default */ $num_rows = mysql_numrows($result); if( $num_rows == 0 ){ return 'Game Not Found!'; } while( $row = mysql_fetch_assoc($result) ) { $current = $row[RId]; $votes = $row[Votes]; } //End current value //What vote if ($vote = down) { function down($current, $votes) { $current--; $votes--; $new = $current/$votes; $new*=100; echo $new; } down()} elseif ($vote = up) { function up($current, $votes) { $current++; $votes++; $new = $current/$votes; $new*=100; echo $new; } up() } else { echo "Hmm... your vote wasnt cast for some reason"; } ?> would this work? Link to comment https://forums.phpfreaks.com/topic/83333-solved-some-php-maths-stuff/ Share on other sites More sharing options...
blueman378 Posted December 27, 2007 Author Share Posted December 27, 2007 well acctually lets scrap the db stuff for now so use; <?php //Retrieve Vote $vote = $_GET['vote']; //Retrieve current value $game = $_GET['game']; global $database; $q = "SELECT * FROM " . Games . " WHERE gName = '$game' ORDER BY `gName` ASC "; $result = $database->query($q) or die("Error: " . mysql_error()); /* Error occurred, return given name by default */ $num_rows = mysql_numrows($result); if( $num_rows == 0 ){ return 'Game Not Found!'; } while( $row = mysql_fetch_assoc($result) ) { $current = $row[RId]; $votes = $row[Votes]; } //End current value //What vote if ($vote = down) { function down($current, $votes) { $current--; $votes--; $new = $current/$votes; $new*=100; echo $new; } down()} elseif ($vote = up) { function up($current, $votes) { $current++; $votes++; $new = $current/$votes; $new*=100; echo $new; } up() } else { echo "Hmm... your vote wasnt cast for some reason"; } ?> Link to comment https://forums.phpfreaks.com/topic/83333-solved-some-php-maths-stuff/#findComment-423961 Share on other sites More sharing options...
blueman378 Posted December 27, 2007 Author Share Posted December 27, 2007 hmm ok well using this: <?php //Retrieve Vote $current = 5; $votes = 22; $vote = $_GET['vote']; //Retrieve current value //End current value //What vote if ($vote = down) { function down() { global $current; global $votes; $current--; $votes--; $new = $current/$votes; $new*=100; echo $new; } down(); } elseif ($vote = up) { function up() { global $current; global $votes; $current++; $votes++; $new = $current/$votes; $new*=100; echo $new; } up(); } else { echo "Hmm... your vote wasnt cast for some reason"; } ?> i get 19.0476190476 using url test.php?vote=up and test.php?vote=down and test.php they all get the same result Link to comment https://forums.phpfreaks.com/topic/83333-solved-some-php-maths-stuff/#findComment-423966 Share on other sites More sharing options...
blueman378 Posted December 27, 2007 Author Share Posted December 27, 2007 ok i got it working with: <?php //Retrieve Vote $vote = $_GET['vote']; $current = 5; $votes = 22; function down() { global $current; global $votes; $current--; $votes--; $new = $current/$votes; $new*=100; echo $new; } function up() { global $current; global $votes; $current++; $votes++; $new = $current/$votes; $new*=100; echo $new; } //Retrieve current value //End current value //What vote if ($vote==down) { down(); } elseif ($vote==up) { up(); } else { echo "Hmm... your vote wasnt cast for some reason"; } ?> but is there any way to round it to the closest whole number? Link to comment https://forums.phpfreaks.com/topic/83333-solved-some-php-maths-stuff/#findComment-423973 Share on other sites More sharing options...
codebyren Posted December 27, 2007 Share Posted December 27, 2007 Right, i'll be honest, I haven't read through your whole code yet cause you and yourself seem to be doing alright so far and posting faster than I can read. Couldn't you use ceil() to round up a number to the next integer number... http://au2.php.net/ceil Sorry, I might be on the completely wrong track here but that's what i would do... Link to comment https://forums.phpfreaks.com/topic/83333-solved-some-php-maths-stuff/#findComment-423978 Share on other sites More sharing options...
blueman378 Posted December 27, 2007 Author Share Posted December 27, 2007 ok thats good to hear, i am not bothering with the rounding now, but now i am trying to use this: if ($new<=19.5) { ?><img src="star/1star.png" width="130" height="25"><?php } elseif ($new>=19.6&&$new>=39.5) { ?> <img src="star/2star.png" width="130" height="25"> <?php } elseif ($new>=39.6&&$new>=59.5) { ?> <img src="star/3star.png" width="130" height="25"> <?php } elseif ($new>=59.6&&$new>=79.5) { ?> <img src="star/4star.png" width="130" height="25"> <?php } elseif ($new>=79.6) { ?> <img src="star/5star.png" width="130" height="25"> <?php } else { echo "Hmm we have no vote..."; } but no matter what $new equals it always shows as one star, any ideas? Link to comment https://forums.phpfreaks.com/topic/83333-solved-some-php-maths-stuff/#findComment-423989 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.