By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,149 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

IF statements not working...

The1corrupted
100+
P: 134
I need help. These if statements don't work like they should... [PHP]
/*Array Info that is being pulled from the room table:
0 = ID
1 = Title
2 = Description

3 = North
4 = South
5 = East
6 = West
7 = Up
8 = Down

9 = X coord
10 = Y coord
11 = Z coord
*/
if ($dir == 1 AND $ray[3] == 1) {
++$_SESSION['xcoord'];
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
} else {
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
if ($dir == 2 AND $ray[5] == 1) {
++$_SESSION['ycoord'];
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
} else {
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
if ($dir == 3 AND $ray[4] == 1) {
--$_SESSION['xcoord'];
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
} else {
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
if ($dir == 4 AND $ray[6] == 1) {
--$_SESSION['ycoord'];
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
} else {
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
if ($dir == 5 AND $ray[7] == 1) {
++$_SESSION['zcoord'];
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
} else {
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
if ($dir == 6 AND $ray[8] == 1) {
--$_SESSION['zcoord'];
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
} else {
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}[/PHP] Am I doing too much coding or would it be something in the array?

$dir = Move command
$ray[number] = 1 or 0 with 1 being TRUE and 0 being FALSE
$_SESSION['x/y/zcoord'] = Coordinate Plane
Feb 22 '07 #1
Share this Question
Share on Google+
16 Replies


ronverdonk
Expert 2.5K+
P: 4,258
I don't understand exactly what is not working. To me the statements are perfectly legal but that is obviously not how you see it.

Maybe what bothers you is: when $dir==6 it will execute all the ELSE branches of if ($dir==1) ...., if ($dir==2) .... etc. Correct?

Ronald :cool:
Feb 22 '07 #2

The1corrupted
100+
P: 134
No, it just flat doesn't work. Those are supposed to be walls, preventing users from walking off the map and into a void. They work perfectly for the Z coords but not for the X and Y...
Feb 22 '07 #3

ronverdonk
Expert 2.5K+
P: 4,258
This is your culprit statement:
[php]if ($dir == 1 AND $ray[3] == 1) {
++$_SESSION['xcoord'];
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
} else {
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
[/php]
Example:
when $dir is not equal to 1 and $ray[3] not equal to 1 you will execute the ELSE branch of this statement. So when your $dir equals 3 and $ray[4] equals 1 it will never get to the statement that tests that condition because the first IF block (shown above) already threw you out.

Ronald :cool:
Feb 22 '07 #4

The1corrupted
100+
P: 134
This is your culprit statement:
[php]if ($dir == 1 AND $ray[3] == 1) {
++$_SESSION['xcoord'];
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
} else {
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
[/php]
Example:
when $dir is not equal to 1 and $ray[3] not equal to 1 you will execute the ELSE branch of this statement. So when your $dir equals 3 and $ray[4] equals 1 it will never get to the statement that tests that condition because the first IF block (shown above) already threw you out.

Ronald :cool:
I don't quite follow you.. When $ray[3] does not equal one, it just goes through the first process like nothing's happened. Should I make it an if statement within an if statement?
Feb 22 '07 #5

ronverdonk
Expert 2.5K+
P: 4,258
I don't quite follow you.. When $ray[3] does not equal one, it just goes through the first process like nothing's happened. Should I make it an if statement within an if statement?
[php]if ($dir == 1 AND $ray[3] == 1) {
++$_SESSION['xcoord'];
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
} else {
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
[/php]
If $dir equals 1 and $ray[3] does not equal 1 it executes the ELSE branch.
If $dir not equals 1 and $ray[3] equals 1 it executes the ELSE branch
if $dir not equals 1 and $ray[3] not equals 1 it executes the ELSE branch.
[php]echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";[/php]

You are testing an AND relation, so when either or both are not true, it will execute the ELSE branch.

Ronald :cool:
Feb 22 '07 #6

The1corrupted
100+
P: 134
Even if I split them into two separate if statements like so:
[PHP]if ($var == 1) {
if ($var2 == 1) {
--/++$_SESSION['coord'];
echo "<meta tag>";
} else {
echo "<meta tag";
}[/PHP]
it still won't work properly.
Feb 22 '07 #7

ronverdonk
Expert 2.5K+
P: 4,258
Why don't you mjust stick with the 'positive' outcome of the if comparisons? Like this:
[php]
if ($dir == 1 AND $ray[3] == 1) {
++$_SESSION['xcoord'];
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
if ($dir == 2 AND $ray[5] == 1) {
++$_SESSION['ycoord'];
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
if ($dir == 3 AND $ray[4] == 1) {
--$_SESSION['xcoord'];
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
if ($dir == 4 AND $ray[6] == 1) {
--$_SESSION['ycoord'];
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
if ($dir == 5 AND $ray[7] == 1) {
++$_SESSION['zcoord'];
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
if ($dir == 6 AND $ray[8] == 1) {
--$_SESSION['zcoord'];
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
}
else {
echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";
} [/php]

Ronald :cool:
Feb 22 '07 #8

The1corrupted
100+
P: 134
Because the roomdisp is a completely separate file and does something entirely different than what is occurring on this page. This page is called more2.php and it is being posted to by admin_input.php. So if moving in a paticular direction is false, then I want to simply refresh roomdisp without changing the session coords.
Feb 22 '07 #9

ronverdonk
Expert 2.5K+
P: 4,258
Because the roomdisp is a completely separate file and does something entirely different than what is occurring on this page. This page is called more2.php and it is being posted to by admin_input.php. So if moving in a paticular direction is false, then I want to simply refresh roomdisp without changing the session coords.
But that is exactly what my snippet is doing. If none of the 'if' statement value combinations validate, script roomdisp.php is called without any $_SESSION['xxx'] updates.

Ronald :cool:
Feb 22 '07 #10

The1corrupted
100+
P: 134
Ah... I'll try it out, then.
Feb 22 '07 #11

The1corrupted
100+
P: 134
Didn't work. Users will still walk straight into a void. *laughs* Those poor souls.
Feb 22 '07 #12

ronverdonk
Expert 2.5K+
P: 4,258
Maybe I don't really understand what you are trying to achieve. My snippet steers your users to [php]"<META HTTP-EQUIV='refresh' content='0; url=roomdisp.php'>";[/php] when none of the conditions is met. Maybe I don't understand what you are trying to achieve. If so, please be patient and explain it, because I am lost here.

Ronald :cool:
Feb 22 '07 #13

The1corrupted
100+
P: 134
Okay... from the ground up...

1. User is in a place

2. User tries to move north (for the sake of example)

3. Query the mysql database to see if that is possible

3a. If possible, user's coordinates change + or - 1 and refresh display

3b. If not possible, user's display simply refreshes and nothing changes.
Feb 22 '07 #14

ronverdonk
Expert 2.5K+
P: 4,258
It is 2.20 am. I quit. See ya tomorrow.

Ronald :cool:
Feb 22 '07 #15

The1corrupted
100+
P: 134
Okay... I'll see if I can get it fixed in the mean time.
Feb 22 '07 #16

The1corrupted
100+
P: 134
Nevermind. I fixed the misbehaving if statements with $_SESSION['vars'] all over the place. I love these little gadgets...
Feb 22 '07 #17

Post your reply

Sign in to post your reply or Sign up for a free account.