Cetanu Posted August 25, 2009 Share Posted August 25, 2009 I have been working on an RPG code and I am trying to make the combat scenario right now. My code is convoluted beyond belief with loads of if statements, but now I have run into a problem where an if statement wont execute if the conditions are right. Here is the code snippet: <?php include "db.php"; $result=mysql_query("SELECT * FROM rpg WHERE player='{$_SESSION['username']}'") or die(mysql_error()); while($row=mysql_fetch_array($result)){ echo "<strong>".$row['name']."</strong><br/> <strong>".$row['species']."</strong><br/> <strong> Health: ".$row['health']."</strong><br/> <strong> Attack: ".$row['attack']."</strong><br/> <strong> Defense: ".$row['defense']."</strong><br/> <strong>"; if($row['species']=="Alien"){ echo "Stealth: ".$row['stealth']; } if($row['species']=="Marine"){ echo "Melee: ".$row['melee']; } echo "</strong> <strong>Money: ".$row['money']."</strong>"; echo "</td><td>"; if(!$_POST['choose']){ echo "Choose an Enemy to Fight <form action='fcenter.php?page=planet1' method='post'> <select size='4' name='fight'> <option value='Aliens'>Aliens</option> <option value='Marines'>Marines</option> <option value='Native Animals'>Native Species</option> <option value='Totally Random'>Randomize</option> </select> <input type='submit' name='choose' value='Search'/> </form>"; } if($_POST['choose']){ if($_POST['fight']=="Aliens"){ $randA = mt_rand(1,20); if($randA=="1" || $randA=="7" || $randA=="2" || $randA=="20"){ echo "You have not found an enemy to fight. Please try again."; } if($randA=="3" || $randA=="6" || $randA=="11" || $randA=="8" || $randA=="16"){ echo "<strong>Enemy:</strong> Drone Alien<br/> <strong>Attack:</strong> 25<br/> <strong>Defense:</strong> 20<br/> <strong>Money for Kill:</strong> 100<br/> <strong>Experience:</strong> 10<br/>"; if(!$_POST['confirm']){ echo "<form action='fcenter.php?page=planet1' method='post'> <input type='radio' name='attack' value='Attack'/>Attack<br/> <input type='radio' name='attack' value='Flee'/> Flee <br/> <input type='submit' name='confirm' value='Choose'/> </form> "; } if($_POST['confirm']){ if($_POST['attack']=="Attack"){ $i = $row['attack']; $money = $row['money']+100; if($i > "20"){ echo "<script>alert('You have damaged the Drone. He currently has 20 health.');</script>"; } } if($i<"20"){ echo "Hi"; } } if($_POST['attack']=="Flee"){ echo "Hi"; } } } } } ?> Here is the part that isn't executing: <?php if($_POST['confirm']){ if($_POST['attack']=="Attack"){ $i = $row['attack']; $money = $row['money']+100; if($i > "20"){ echo "<script>alert('You have damaged the Drone. He currently has 20 health.');</script>"; } } if($i<"20"){ echo "Hi"; } } if($_POST['attack']=="Flee"){ echo "Hi"; } } ?> It says echo "hi" for flee 'cause I was seeing if that would even work, but nooooo it didn't. All it does is redirect me to the page that it executes on. Quote Link to comment https://forums.phpfreaks.com/topic/171725-if-statement-not-working/ Share on other sites More sharing options...
Philip Posted August 25, 2009 Share Posted August 25, 2009 Looks like you have some mismatched brackets. You could easily tell/fix this by indenting your code properly. See how it looks indented: <?php if($_POST['confirm']){ if($_POST['attack']=="Attack"){ $i = $row['attack']; $money = $row['money']+100; if($i > "20"){ echo "<script>alert('You have damaged the Drone. He currently has 20 health.');</script>"; } } if($i<"20"){ echo "Hi"; } } if($_POST['attack']=="Flee"){ echo "Hi"; } } // extra ?> Quote Link to comment https://forums.phpfreaks.com/topic/171725-if-statement-not-working/#findComment-905493 Share on other sites More sharing options...
Cetanu Posted August 25, 2009 Author Share Posted August 25, 2009 Oh. I never indented because my editor doesn't allow [TAB], but oh well, I'll just put it into Notepad and do it. I still don't think that's it because when I mismatch my brackets I always get a fatal error-type thing. I just realized that the bracket labeled "extra" is actually for a statement from before that snippet. You can see it in the whole code. Quote Link to comment https://forums.phpfreaks.com/topic/171725-if-statement-not-working/#findComment-905495 Share on other sites More sharing options...
deadlyp99 Posted August 25, 2009 Share Posted August 25, 2009 It's possible you have a data type mismatch. Try instead of $i<"20", using $i<20. It just depends on how it's stored in the database though. Quote Link to comment https://forums.phpfreaks.com/topic/171725-if-statement-not-working/#findComment-905496 Share on other sites More sharing options...
Cetanu Posted August 25, 2009 Author Share Posted August 25, 2009 Okay, I'll give that a shot too. Quote Link to comment https://forums.phpfreaks.com/topic/171725-if-statement-not-working/#findComment-905497 Share on other sites More sharing options...
Cetanu Posted August 25, 2009 Author Share Posted August 25, 2009 Okay I've indented everything so that it will make it easier to reference. <?php include "db.php"; $result=mysql_query("SELECT * FROM rpg WHERE player='{$_SESSION['username']}'") or die(mysql_error()); while($row=mysql_fetch_array($result)){ echo "<strong>".$row['name']."</strong><br/> <strong>".$row['species']."</strong><br/> <strong> Health: ".$row['health']."</strong><br/> <strong> Attack: ".$row['attack']."</strong><br/> <strong> Defense: ".$row['defense']."</strong><br/> <strong>"; if($row['species']=="Alien"){ echo "Stealth: ".$row['stealth']; } if($row['species']=="Marine"){ echo "Melee: ".$row['melee']; } echo "</strong> <strong>Money: ".$row['money']."</strong>"; echo "</td><td>"; if(!$_POST['choose']){ echo "Choose an Enemy to Fight <form action='fcenter.php?page=planet1' method='post'> <select size='4' name='fight'> <option value='Aliens'>Aliens</option> <option value='Marines'>Marines</option> <option value='Native Animals'>Native Species</option> <option value='Totally Random'>Randomize</option> </select> <input type='submit' name='choose' value='Search'/> </form>"; } if($_POST['choose']){ if($_POST['fight']=="Aliens"){ $randA = mt_rand(1,20); if($randA=="1" || $randA=="7" || $randA=="2" || $randA=="20"){ echo "You have not found an enemy to fight. Please try again."; } if($randA=="3" || $randA=="6" || $randA=="11" || $randA=="8" || $randA=="16"){ echo "<strong>Enemy:</strong> Drone Alien<br/> <strong>Attack:</strong> 25<br/> <strong>Defense:</strong> 20<br/> <strong>Money for Kill:</strong> 100<br/> <strong>Experience:</strong> 10<br/>"; if(!$_POST['confirm']){ echo "<form action='fcenter.php?page=planet1' method='post'> <input type='radio' name='attack' value='Attack'/>Attack<br/> <input type='radio' name='attack' value='Flee'/> Flee <br/> <input type='submit' name='confirm' value='Choose'/> </form> "; } if($_POST['confirm']){ if($_POST['attack']=="Attack"){ $i = $row['attack']; $money = $row['money']+100; if($i>20){ echo "<script>alert('You have damaged the Drone. He currently has 20 health.');</script>"; } if($i<20){ echo "Hi"; } } if($_POST['attack']=="Flee"){ echo "Hi"; } } } } } } echo "</td></tr></table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/171725-if-statement-not-working/#findComment-905501 Share on other sites More sharing options...
Cetanu Posted August 25, 2009 Author Share Posted August 25, 2009 Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/171725-if-statement-not-working/#findComment-906001 Share on other sites More sharing options...
mikesta707 Posted August 25, 2009 Share Posted August 25, 2009 you cant compare strings as if they were numbers so $i < "20" won't work. It will compare it lexicographically (I think) which will result in unwanted results (Like 7 being greater than 100); if you want to compare a string as a number, I suggest you use the parseInt function. so instead of things like if ($i < "20") do if(parseInt($i) < 20) Quote Link to comment https://forums.phpfreaks.com/topic/171725-if-statement-not-working/#findComment-906007 Share on other sites More sharing options...
Cetanu Posted August 25, 2009 Author Share Posted August 25, 2009 That's new! I'll try it. Quote Link to comment https://forums.phpfreaks.com/topic/171725-if-statement-not-working/#findComment-906062 Share on other sites More sharing options...
kenrbnsn Posted August 25, 2009 Share Posted August 25, 2009 There is no parseInt function in PHP. Ken Quote Link to comment https://forums.phpfreaks.com/topic/171725-if-statement-not-working/#findComment-906066 Share on other sites More sharing options...
mikesta707 Posted August 25, 2009 Share Posted August 25, 2009 Oh dude... my bad.... Sorry been doing a lot of javascript lately so I mix them up alot, hold on lemme find a function thats equivalent yeah use intval() instead of parseInt. again my bad lol, been mixing php and javascript a lot lately so they both have kind of meshed together in my brain. Quote Link to comment https://forums.phpfreaks.com/topic/171725-if-statement-not-working/#findComment-906073 Share on other sites More sharing options...
Cetanu Posted August 25, 2009 Author Share Posted August 25, 2009 Oh, okay. It's fine. Quote Link to comment https://forums.phpfreaks.com/topic/171725-if-statement-not-working/#findComment-906109 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.