Choosing a database table dynamically by the value of a session variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rlm51
    New Member
    • Mar 2007
    • 29

    Choosing a database table dynamically by the value of a session variable

    I have a database with several tables designating different product categories. Within each table is many rows representing different products. I want to be able to choose product category table dynamically in a conditional statement based on the value of a session variable. I am using php in dreamweaver 8.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    Have you made any attempt at this yourself?
    Could you explain what you have tried and why (how) it didn't work?

    Please show us your code and your database structure.

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      I also do not really understand the problem, but I don't know Dreamweaver. In pure PHP it would be something like:
      [php]
      $tabname =$_SESSION['category'];
      $sql_stmt="SELE CT * FROM $tabname WHERE etc.......";
      [/php]
      Or don't I fully understand your problem?

      Ronald

      Comment

      • rlm51
        New Member
        • Mar 2007
        • 29

        #4
        Sorry ... I am fairly new at writing code outside of Dreamweavers built in server behaviors ...

        I took your advice and

        [code=php]
        if ($_SESSION['skey'] = 1)
        THEN ($tblname = ('tbl_1'));

        elseif ($_SESSION['skey'] = 2)
        THEN ($tblname = ('tbl_2'));

        else ($tblname = ('tbl_3'));

        end if;
        [/code]
        then do the sql insert into $tblname where key =; etc ...

        However, I am getting this error:

        Parse error: parse error, unexpected T_ENDIF in /home/content/w/o/o/woodwebsite/html/DeptSpecials/InsertData2.php on line 85


        Do I have a syntax problem?

        Thanks everyone for your help ... I am getting better!
        Last edited by Atli; Oct 3 '07, 12:11 AM. Reason: Added [code] tags.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          The problem is in your if syntax.

          You can read about the correct usage here, at php.net.

          Here is a simple example of the correct usage:
          [code=php]
          <?php
          $number = 2;

          if($number < 5) {
          echo "The number is less than five!";
          }
          else if($number >5) {
          echo "The number is more than five!";
          }
          else {
          echo "The number IS five!!";
          }
          ?>
          [/code]

          Comment

          • nathj
            Recognized Expert Contributor
            • May 2007
            • 937

            #6
            Hi,

            The IF structure is great when you want to branch the code in two. However, what you are doing is branching the code in many ways - there are more than two tables that could be selected. You would be better off using the SWITCH structure:

            [php]
            <?php

            swith ($_SESSION['skey'])
            {
            case 1:
            $tblname = 'tbl_1' ;
            break ;
            case 2:
            $tblname = 'tbl_2' ;
            break ;
            case 3:
            $tblname = 'tbl_3' ;
            break ;
            case 4:
            $tblname = 'tbl_4' ;
            break ;
            default:
            $tblname = 'tbl_5' ;
            break ;
            }
            ?>
            [/php]
            This would set the variable for you ready for what comes next. It is, as you can see, easier to understand and therefore maintain than a long if elseif... style structure.

            Cheers
            nathj

            Comment

            • rlm51
              New Member
              • Mar 2007
              • 29

              #7
              Hey ...

              I used the case statement and it worked. I did have to put it right at the top under the first

              <?php require_once('. ./Connections/conn_specials.p hp'); ?>
              <?php

              It worked perfectly, and will provide the key to this whole application ... that's the second time I have gotten a solution from this forum. I am very grateful.

              Thank you to Atli, Ronverdonk, and Nathj ...

              Robert

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                Originally posted by rlm51
                ...... Thank you to Atli, Ronverdonk, and Nathj ...

                Robert
                That is why our community is here, to help each other.

                Ronald

                Comment

                • nathj
                  Recognized Expert Contributor
                  • May 2007
                  • 937

                  #9
                  Hi,

                  I'm happy to have helped, it's a great forum you're right. I get loads of help from here so I'm happy to have given some back.

                  Cheers
                  nathj

                  Comment

                  Working...