6 Replies Latest reply on Nov 5, 2010 5:40 AM by rongrace1

    IF Statement

    rongrace1
      I've created a login page in  Dreamweaver and there is a redirect page for login success and login  fail. What I want to have is a selection of screens for login success based on a data  field held in the user security table.
      Each security record will  have up to 5 different security codes e.g. code1, code2 etc. If a code  e.g. TA40, appears in any of the 5 code fields then I want the user  directed to one page, if code TA41 appears in any of the 5 code fields  then I want them directed to another page.
      The user code will only  appear in one of the fields, but can appear in any of the 5 code  fields.. How can I incorporate this in to the login screen logic, as  currently it will just show the following

       

      if (isset($_POST['opno'])) {
        $loginUsername=$_POST['opno'];
        $password=$_POST['pass_word'];
        $MM_fldUserAuthorization = "";
        $MM_redirectLoginSuccess = "/menu.php";
        $MM_redirectLoginFailed = "/loginform.php";
        $MM_redirecttoReferrer = false;
        mysql_select_db($database_Hanson, $Hanson);

       

        • 1. Re: IF Statement
          w1n78 Level 2

          let's say your column name is security_code. use a switch statement

           

          <?php
          ... user has been verified and has is allowed access
          switch ($row['security_code']) {
            case 'TA40':
              // actions for TA40
            break;
          
            case 'TA41':
              // actions for TA41
            break;
          
            // optional but good for a "catch all"
            default:
              // invalid security code
            break;
          }
          ?>
          

           

          using switch is like using a bunch of nested if statements

          • 2. Re: IF Statement
            rongrace1 Level 1

            In your example you use a data item security_code, what if it needs to check more than 1 data item.

            In my example it needs to check 5 fields.

             

            The dataset is structured as follows

             

            User_no

            User_name

            Pass_word

            User_type

            Tlist1

            Tlist2

            Tlist3

            Tlist4

            Tlist5

             

            The security code 'TA40' can exist in any of the Tlist fields, but it will only exist in one. Other codes may appear in the other fields, for example records could look like this

             

            001, Joe Bloggs, test, U, TA40, TE01, VS22, 0, 0

            002, Fred Bloggs, test1, U, AJ01, VS20, IQ01, TA40, 0

             

            So I need to be able to check each Tlist field for the possibility of the the ocurrence and show the user a certain menu depending on their Tlist code

            • 3. Re: IF Statement
              w1n78 Level 2

              that's a weird db design but you can approach this in more than one way. i would store the row in an array. you need to modify the query to log in. remember modifying DW's code will remove the behavior from the interface. the new login query should be

               

              SELECT * FROM table
              

               

              here's a sample code.

               

              <?php
              if (isset($_POST['username'])) {
                $sql_login = sprintf("SELECT * FROM users WHERE User_name = '%s' AND Pass_word = '%s'",
                  mysql_real_escape_string($_POST['username']), mysql_real_escape_string($_POST['password']));
              
                $rs_login = mysql_query($sql_login);
              
                if ($rs_login) {
                  if (mysql_num_rows($rs_login) > 0) {         // if user found
                    $row_login = mysql_fetch_assoc($rs_login);
                    // array
                    $result_array = array(
                      $row_login['User_no'],
                      $row_login['User_name'],
                      $row_login['Pass_word'],
                      $row_login['User_type'],
                      $row_login['Tlist1'],
                      $row_login['Tlist2'],
                      ...
                    );
              
                    // create session
                    $_SESSION['MM_UserID'] = $row_login['User_no'];
                    $_SESSION['MM_Username'] = $row_login['User_name'];
              
                    // use in_array() to determine if value exist
                    if (in_array("TA40", $result_array)) {
                      header('location: page1.php');
                      exit();
                    }
              
                    if (in_array("TA41", $result_array)) {
                      header('location: page2.php');
                      exit();
                    }
              
                    ...
              
              
                  }
                }
              }
              ?>
              

               

              i haven't used DW's code in a long time. i've been using PDO so i tried my best to write the code as close to how DW and PHP would write it. i'm guessing your Tlist columns have a one-to-many relationship to a user - meaning a user can having many Tlist? you should reconsider the db design if possible so that you can take full advantage of mysql and relational databases (but that's a different discussion ) hope the above sample code makes sense

              • 4. Re: IF Statement
                rongrace1 Level 1

                Apologies for the delay in getting back to you, I've been a

                tied up with work.

                I'll try and rebuild the database using an array item rather than multiple fields

                • 5. Re: IF Statement
                  w1n78 Level 2

                  you don't have to rebuild the db. the example i showed you is using the existing layout that you shared.

                  • 6. Re: IF Statement
                    rongrace1 Level 1

                    Cheers