blueman378 Posted December 27, 2007 Share Posted December 27, 2007 hi guys, i have this code here <?php //Retrieve Vote $vote = $_GET['vote']; $current = 49; $votes = 99; function down() { global $current; global $votes; $current--; $votes--; $new = $current/$votes; $new*=100; echo "This was voted:"; echo $new; } function up() { global $current; global $votes; $current++; $votes++; $new = $current/$votes; $new*=100; echo "This was voted:"; 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"; } 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 still says only one star, any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/83342-faulty-if-statement/ Share on other sites More sharing options...
shocker-z Posted December 27, 2007 Share Posted December 27, 2007 Try this mate <?php //Retrieve Vote $vote = $_GET['vote']; $current = 49; $votes = 99; function down() { global $current; global $votes; $current--; $votes--; $new = $current/$votes; $new*=100; echo "This was voted:"; echo $new; } function up() { global $current; global $votes; $current++; $votes++; $new = $current/$votes; $new*=100; echo "This was voted:"; 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"; } 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..."; } ?> I haven't checked code above the if statements.. Regards Liam Quote Link to comment https://forums.phpfreaks.com/topic/83342-faulty-if-statement/#findComment-424020 Share on other sites More sharing options...
kenrbnsn Posted December 27, 2007 Share Posted December 27, 2007 How can any of your "elseif" clauses work with this: <?php elseif (($new>=39.6)&&($new>=59.5)) { ?> You are saying that both conditions have to be true -- $new greater than or equal to two numbers -- impossible. Also, your functions don't return anything, so the variable $new isn't set to anything. I think what you want is: <?php //Retrieve Vote $vote = $_GET['vote']; $current = 49; $votes = 99; function down($cur,$vts) { $cur--; $vts--; $new = $cur/$vts; $new*=100; return (array($cur,$vts,$new); } function up($cur,$vts) { $cur++; $vts++; $new = $cur/$vts; $new*=100; return (array($cur,$vts,$new) } //Retrieve current value //End current value //What vote if ($vote == 'down') { list($current,$votes,$new) = down($current,$votes); } elseif ($vote==up) { list($current,$vots,$new) = up($current,$votes); } else { echo "Hmm... your vote wasnt cast for some reason"; } $errmsg = ''; if ($new <= 19.5) $star = 1; elseif ($new >= 19.6 && $new <= 39.5) $star = 2; elseif ($new >= 39.6 && $new <= 59.5) $star = 3; elseif ($new >= 59.6 && $new <= 79.5) $star = 4 elseif ($new >= 79.6) $star = 5; else $errmsg = "Hmm we have no vote..."; if ($errmsg != '') echo $errmsg; else echo '<img src="star/' . $star . 'star.png" width="130" height="25">'; ?> Notice how I also streamlined your code. Ken Quote Link to comment https://forums.phpfreaks.com/topic/83342-faulty-if-statement/#findComment-424051 Share on other sites More sharing options...
Barand Posted December 27, 2007 Share Posted December 27, 2007 $new isn't defined. Return the calculated value from the up() and down() functions <?php //Retrieve Vote $vote = $_GET['vote']; $current = 49; $votes = 99; function down() { global $current; global $votes; $current--; $votes--; $new = $current/$votes; $new*=100; return $new; } function up() { global $current; global $votes; $current++; $votes++; $new = $current/$votes; $new*=100; return $new; } //Retrieve current value //End current value //What vote if ($vote==down) { $new = down(); // assign value to $new } elseif ($vote==up) { $new = up(); // assign value to $new } else { echo "Hmm... your vote wasnt cast for some reason"; } echo "This was voted: $new"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83342-faulty-if-statement/#findComment-424052 Share on other sites More sharing options...
blueman378 Posted December 28, 2007 Author Share Posted December 28, 2007 hey ken, thanks for that after some minor errors it worked perfectly <?php //Retrieve Vote $vote = $_GET['vote']; $current = 49; $votes = 99; function down($cur,$vts) { $cur--; $vts--; $new = $cur/$vts; $new*=100; return (array($cur,$vts,$new)); } function up($cur,$vts) { $cur++; $vts++; $new = $cur/$vts; $new*=100; return (array($cur,$vts,$new)); } //Retrieve current value //End current value //What vote if ($vote == 'down') { list($current,$votes,$new) = down($current,$votes); } elseif ($vote==up) { list($current,$vots,$new) = up($current,$votes); } else { echo "Hmm... your vote wasnt cast for some reason"; } $errmsg = ''; if ($new <= 19.5) $star = 1; elseif ($new >= 19.6 && $new <= 39.5) $star = 2; elseif ($new >= 39.6 && $new <= 59.5) $star = 3; elseif ($new >= 59.6 && $new <= 79.5) $star = 4; elseif ($new >= 79.6) $star = 5; else $errmsg = "Hmm we have no vote..."; if ($errmsg != '') echo $errmsg; else echo '<img src="star/' . $star . 'star.png" width="130" height="25">'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83342-faulty-if-statement/#findComment-424568 Share on other sites More sharing options...
blueman378 Posted December 28, 2007 Author Share Posted December 28, 2007 ok guys so this is working now, <hr> <h1>Rating:</h1> <?php //Retrieve Vote $vote = $_GET['vote']; $current = 1; $votes = 1; function down($cur,$vts) { $cur--; $vts++; $new = $cur/$vts; $new*=100; return (array($cur,$vts,$new)); } function up($cur,$vts) { $cur++; $vts++; $new = $cur/$vts; $new*=100; return (array($cur,$vts,$new)); } //Retrieve current value //End current value //What vote if ($vote == 'down') { list($current,$votes,$new) = down($current,$votes); } elseif ($vote==up) { list($current,$vots,$new) = up($current,$votes); } else { echo "Hmm... your vote wasnt cast for some reason"; } $errmsg = ''; if ($new <= 19.5) $star = 1; elseif ($new >= 19.6 && $new <= 39.5) $star = 2; elseif ($new >= 39.6 && $new <= 59.5) $star = 3; elseif ($new >= 59.6 && $new <= 79.5) $star = 4; elseif ($new >= 79.6) $star = 5; else $errmsg = "Hmm we have no vote..."; if ($errmsg != '') echo $errmsg; else echo 'This was Rated '. $new .' from '. $votes.' votes<br> so is a <img src="star/' . $star . 'star.png" width="260" height="50"> Game'; ?> but there is only one problem with it, all games start as 1 star, is there any way i could make this so they all start as three star? Quote Link to comment https://forums.phpfreaks.com/topic/83342-faulty-if-statement/#findComment-424577 Share on other sites More sharing options...
blueman378 Posted December 28, 2007 Author Share Posted December 28, 2007 well heres my final code so it adds does the database stuff, and i you have not voted then it shows the voting thing, <hr> <h1>Rating:</h1> <?php //Retrieve Vote //Retrieve current value $game = $_GET['game']; $vote = $_GET['vote']; 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 function down($cur,$vts) { global $database; global $game; $cur--; $vts++; $new = $cur/$vts; $new*=100; $q1 = "UPDATE " . Games . " SET RId = '$cur' WHERE gName = '$game'"; $result = $database->query($q1) or die("Error: " . mysql_error()); $q2 = "UPDATE " . Games . " SET Votes = '$vts' WHERE gName = '$game'"; $result = $database->query($q2) or die("Error: " . mysql_error()); return (array($cur,$vts,$new)); } function up($cur,$vts) { global $database; global $game; $cur++; $vts++; $new = $cur/$vts; $new*=100; $q1 = "UPDATE " . Games . " SET RId = '$cur' WHERE gName = '$game'"; $result = $database->query($q1) or die("Error: " . mysql_error()); $q2 = "UPDATE " . Games . " SET Votes = '$vts' WHERE gName = '$game'"; $result = $database->query($q2) or die("Error: " . mysql_error()); return (array($cur,$vts,$new)); } function none($cur,$vts) { global $database; global $game; $new = $cur/$vts; $new*=100; return (array($cur,$vts,$new)); } //Retrieve current value //End current value //What vote if ($vote == 'down') { list($current,$votes,$new) = down($current,$votes); } elseif ($vote==up) { list($current,$votes,$new) = up($current,$votes); } else { list($current,$votes,$new) = none($current,$votes); echo "<b>Have your say:</b> <a href='showgame.php?game=".$game."&vote=up'> <img src='images/good.png'></a> | <a href='showgame.php?game=".$game."&vote=down'> <img src='images/bad.png'></a><br><hr>"; } $errmsg = ''; if ($new <= 19.5) $star = 1; elseif ($new >= 19.6 && $new <= 39.5) $star = 2; elseif ($new >= 39.6 && $new <= 59.5) $star = 3; elseif ($new >= 59.6 && $new <= 79.5) $star = 4; elseif ($new >= 79.6) $star = 5; else $errmsg = "Hmm we have no vote..."; if ($errmsg != '') echo $errmsg; else echo 'This was Rated '. $new .' from '. $votes.' votes<br> so is a <img src="star/' . $star . 'star.png" width="260" height="50"> Game'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/83342-faulty-if-statement/#findComment-424602 Share on other sites More sharing options...
Barand Posted December 28, 2007 Share Posted December 28, 2007 $q1 = "UPDATE " . Games . " SET RId = '$cur' WHERE gName = '$game'"; $result = $database->query($q1) or die("Error: " . mysql_error()); $q2 = "UPDATE " . Games . " SET Votes = '$vts' WHERE gName = '$game'"; $result = $database->query($q2) or die("Error: " . mysql_error()); You only need 1 query <?php $q1 = "UPDATE Games SET RId = '$cur', Votes = '$vts' WHERE gName = '$game'"; $result = $database->query($q1) or die("Error: " . mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/83342-faulty-if-statement/#findComment-424604 Share on other sites More sharing options...
blueman378 Posted December 28, 2007 Author Share Posted December 28, 2007 thanks, much tidier:) Quote Link to comment https://forums.phpfreaks.com/topic/83342-faulty-if-statement/#findComment-424607 Share on other sites More sharing options...
blueman378 Posted December 28, 2007 Author Share Posted December 28, 2007 oh one question, any idea where i would put it so that if $new is over 100 set it too 100 or below zero set to zero? thanks Quote Link to comment https://forums.phpfreaks.com/topic/83342-faulty-if-statement/#findComment-424609 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.