Jump to content

if() statement not working


Cetanu

Recommended Posts

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.

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";
?>

 

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.