IF statements not working...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • The1corrupted
    New Member
    • Feb 2007
    • 134

    IF statements not working...

    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.ph p'>";
    } else {
    echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
    }
    if ($dir == 2 AND $ray[5] == 1) {
    ++$_SESSION['ycoord'];
    echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
    } else {
    echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
    }
    if ($dir == 3 AND $ray[4] == 1) {
    --$_SESSION['xcoord'];
    echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
    } else {
    echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
    }
    if ($dir == 4 AND $ray[6] == 1) {
    --$_SESSION['ycoord'];
    echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
    } else {
    echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
    }
    if ($dir == 5 AND $ray[7] == 1) {
    ++$_SESSION['zcoord'];
    echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
    } else {
    echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
    }
    if ($dir == 6 AND $ray[8] == 1) {
    --$_SESSION['zcoord'];
    echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
    } else {
    echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
    }[/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
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    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:

    Comment

    • The1corrupted
      New Member
      • Feb 2007
      • 134

      #3
      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...

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        This is your culprit statement:
        [php]if ($dir == 1 AND $ray[3] == 1) {
        ++$_SESSION['xcoord'];
        echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
        } else {
        echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
        }
        [/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:

        Comment

        • The1corrupted
          New Member
          • Feb 2007
          • 134

          #5
          Originally posted by ronverdonk
          This is your culprit statement:
          [php]if ($dir == 1 AND $ray[3] == 1) {
          ++$_SESSION['xcoord'];
          echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
          } else {
          echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
          }
          [/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?

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Originally posted by The1corrupted
            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.ph p'>";
            } else {
            echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
            }
            [/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.ph p'>";[/php]

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

            Ronald :cool:

            Comment

            • The1corrupted
              New Member
              • Feb 2007
              • 134

              #7
              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.

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                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.ph p'>";
                }
                if ($dir == 2 AND $ray[5] == 1) {
                ++$_SESSION['ycoord'];
                echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
                }
                if ($dir == 3 AND $ray[4] == 1) {
                --$_SESSION['xcoord'];
                echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
                }
                if ($dir == 4 AND $ray[6] == 1) {
                --$_SESSION['ycoord'];
                echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
                }
                if ($dir == 5 AND $ray[7] == 1) {
                ++$_SESSION['zcoord'];
                echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
                }
                if ($dir == 6 AND $ray[8] == 1) {
                --$_SESSION['zcoord'];
                echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
                }
                else {
                echo "<META HTTP-EQUIV='refresh' content='0; url=roomdisp.ph p'>";
                } [/php]

                Ronald :cool:

                Comment

                • The1corrupted
                  New Member
                  • Feb 2007
                  • 134

                  #9
                  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.

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    Originally posted by The1corrupted
                    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:

                    Comment

                    • The1corrupted
                      New Member
                      • Feb 2007
                      • 134

                      #11
                      Ah... I'll try it out, then.

                      Comment

                      • The1corrupted
                        New Member
                        • Feb 2007
                        • 134

                        #12
                        Didn't work. Users will still walk straight into a void. *laughs* Those poor souls.

                        Comment

                        • ronverdonk
                          Recognized Expert Specialist
                          • Jul 2006
                          • 4259

                          #13
                          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.ph p'>";[/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:

                          Comment

                          • The1corrupted
                            New Member
                            • Feb 2007
                            • 134

                            #14
                            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.

                            Comment

                            • ronverdonk
                              Recognized Expert Specialist
                              • Jul 2006
                              • 4259

                              #15
                              It is 2.20 am. I quit. See ya tomorrow.

                              Ronald :cool:

                              Comment

                              Working...