seany123 Posted April 27, 2009 Share Posted April 27, 2009 ive got so confused and i dont know which of these should be ifs and which should be else ifs... <?php ///////////////////////////////////////// AWAKE 10%////////////////////////////////////////// if ($player->nerve >= $nerveloss && $player->awake >= $player->maxawake * 0 && $player->awake < $player->maxawake * 0.20) { //sucess / fail. $random1 = rand(1,1000); //Prison. $random2 = rand(1,100); //sucess if($random1 <= 76){ if ($player->exp + $expgain < $player->maxexp) { //Update player (the Winner) $query = $db->execute("update `players` set `nerve`=?,`money`=?, `exp`=? where `id`=?", array($player->nerve - $nerveloss, $player->money + $moneygain, $player->exp + $expgain, $player->id)); //Update enemy (the Loser) $query = $db->execute("update `players` set `money`=? where `id`=?", array($enemy->money - $moneygain, $enemy->id)); //Add log message for enemy $logmsg = "<a href=\"profile.php?id=" . $player->username . "\">" . $player->username . "</a> mugged you and stole " . $number_format($moneygain) . "<br />"; addlog($enemy->id, $logmsg, $db); //Add output for the player echo "You mugged " . $enemy->username . " successfully."; echo "<br> You gained " . $expgain . " and $" . $english_format_number = number_format ($moneygain) . "."; echo "<br><br>"; echo "<a href=\"home.php\">Back</a>"; } else if ($player->exp + $expgain >= $player->maxexp) { //Update player (the Winner) $query = $db->execute("Update `players` set `level`= `level`+?, `maxexp` =?, `exp` =?, `money` = `money` + ?, `hp`=?, `maxhp`=?,`maxenergy`=`maxenergy`+?, `energy`=?, `crimes_sucess`=?, `crimes_money`=?, `maxnerve`=? where `id`=?",array(1, $player->maxexp - $player->maxexp + $maxexpgain, $newexp, $player->money + $moneygain, $player->maxhp + 10, $player->maxhp + 10, $player->maxenergy + 1, $player->energy = $player->maxenergy, $player->crimes_sucess + 1, $player->crimes_money + $moneygain, $player->maxnerve + 1, $player->id)); //update enemy (the loser) $query = $db->execute("update `players` set `money`=?, `id`=?", array($enemy->money - $moneygain, $enemy->id)); //Add log message for enemy $logmsg = "<a href=\"profile.php?id=" . $player->username . "\">" . $player->username . "</a> mugged you and stole " . $number_format($moneygain) . "<br />"; addlog($enemy->id, $logmsg, $db); //Add output for the player echo "You mugged " . $enemy->username . " successfully."; echo "<br> You gained " . $expgain . " and $" . $english_format_number = number_format ($moneygain) . "."; echo "<br><br>"; echo "<a href=\"home.php\">Back</a>"; } } //Fail if ($random1 >= 77 && $random2 >= 60){ //update player (the Loser) $query = $db->execute("update `players` set `nerve`=?, `crimes_failed`=? where `id`=?", array($player->nerve - $nerveloss, $player->crimes_failed + 1, $player->id )); //Add log message for enemy $logmsg = "<a href=\"profile.php?id=" . $player->username . "\">" . $player->username . "</a> mugged you but failed stole <br />"; addlog($enemy->id, $logmsg, $db); //Add output for the player echo "<br />You failed to mug " . $enemy->username . "<br />"; echo "<br><br>"; echo "<a href=\"home.php\">Back</a>"; } //Fail & Prison else if($random2 >= 77 && $random2 <= 59){ //update player (the Loser) $query = $db->execute("update `players` set `nerve`=?, `prison`=?, `crimes_failed`=? where `id`=?", array($player->nerve - $nerveloss, $player->prison + $prisontime, $player->crimes_failed + 1,$player->id )); //Add log message for enemy $logmsg = "<a href=\"profile.php?id=" . $player->username . "\">" . $player->username . "</a> mugged you but failed stole <br />"; addlog($enemy->id, $logmsg, $db); //Add output for the player echo "You failed to mug " . $enemy->username . "."; echo "You was sent to prison."; echo "<br><br>"; echo "<a href=\"prison.php\">Prison</a>"; } } please could someone look at this and let me know which should be which? thanks Link to comment https://forums.phpfreaks.com/topic/155905-solved-ifs-and-else-ifs/ Share on other sites More sharing options...
Mchl Posted April 27, 2009 Share Posted April 27, 2009 How we should know? It's you who decides how your program executes. Link to comment https://forums.phpfreaks.com/topic/155905-solved-ifs-and-else-ifs/#findComment-820673 Share on other sites More sharing options...
seany123 Posted April 27, 2009 Author Share Posted April 27, 2009 well then can anyone see any errors? because for some reason it wont echo anything other than the if ($random1 >= 77 && $random2 >= 60){ line. Link to comment https://forums.phpfreaks.com/topic/155905-solved-ifs-and-else-ifs/#findComment-820675 Share on other sites More sharing options...
Cosizzle Posted April 27, 2009 Share Posted April 27, 2009 Sometimes something we loose sight of quite easily is WHY we use something, or we forget the basics of what it does. <?php $yourName = 'sam' if ($yourName == 'bob') { echo 'hi bob'; } else if ($yourName == 'jill') { echo 'hi jill'; } else { echo 'I dont know you'; } //OUTPUT: I dont know you ?> why you may never see <?php if ($random1 >= 77 && $random2 >= 60){ ?> is because of your odds... //sucess / fail. $random1 = rand(1,1000); //Prison. $random2 = rand(1,100); for testing purposes maybe bring $random1 down to 100 Link to comment https://forums.phpfreaks.com/topic/155905-solved-ifs-and-else-ifs/#findComment-820687 Share on other sites More sharing options...
Mchl Posted April 27, 2009 Share Posted April 27, 2009 This condition: else if($random2 >= 77 && $random2 <= 59){ will never be true Link to comment https://forums.phpfreaks.com/topic/155905-solved-ifs-and-else-ifs/#findComment-820692 Share on other sites More sharing options...
Cosizzle Posted April 27, 2009 Share Posted April 27, 2009 heh ya just saw that too... i knew it was something in that little block there Link to comment https://forums.phpfreaks.com/topic/155905-solved-ifs-and-else-ifs/#findComment-820699 Share on other sites More sharing options...
seany123 Posted April 27, 2009 Author Share Posted April 27, 2009 THANKYOOOOUUU!! finally its working right you sont realise how long i been messing around to make this code work! lol. cheers everyone... great help Link to comment https://forums.phpfreaks.com/topic/155905-solved-ifs-and-else-ifs/#findComment-820717 Share on other sites More sharing options...
Cosizzle Posted April 27, 2009 Share Posted April 27, 2009 Just break it down when something isnt working. Make simple tests Link to comment https://forums.phpfreaks.com/topic/155905-solved-ifs-and-else-ifs/#findComment-820761 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.