Need help with switch statment

Discussion in 'PHP' started by absentx, Mar 5, 2011.

  1. #1
    Okay so I still consider myself fairly new to php but I am making good progress.

    I have a few questions about the switch statement.

    I want to pull different cases from a database but I am having trouble getting the syntax right..or maybe this is just not the proper way to do it?? Basically I have a switch statment that analyzes the "$trigger" variable...but I have several different cases that the "$trigger" variable could be...rather than writing each line of code in, I was hoping I could just pull all the different cases from a database.

    Could someone give me an example or get me headed in the proper direction with perhaps a different way to code the whole situation?
     
    absentx, Mar 5, 2011 IP
  2. eleetgeek

    eleetgeek Peon

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It is tricky. Switch case will not allow loop within switch. You will need to use else if and if else instead of switch that way u can have a loop.
    Thats the only workaround. However, let me see if someone comes up with a brilliant solution altogether!
     
    eleetgeek, Mar 5, 2011 IP
  3. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #3
    I suppose youve got a database with two collums, one with the case, and one with the code?

    If so, you could just select the right case from the database, right?
     
    ssmm987, Mar 6, 2011 IP
  4. dgreenhouse

    dgreenhouse Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Give an example of what you're trying to accomplish with the switch statement.

    Partial code will help.
     
    dgreenhouse, Mar 6, 2011 IP
  5. ipr22

    ipr22 Peon

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    loop through the database to get all the possible trigger values, then afterwards do the switch
     
    ipr22, Mar 6, 2011 IP
  6. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes that is exactly what I am trying to do, but I haven't gotten it to work yet.

    What I currently have:

    
    switch($trigger){
             case 'a':
             case 'b':
             case 'c':
    }
    
    PHP:
    What I would like to do:

    
    
    while ($row = mysqli_fetch_array($data)) {
    //maybe place the values in an array or something????
    
    switch($trigger){
             case 'somehow get all the values from the array or the database to here':
            }
    
    PHP:
    The reason I want to do this is because I have a backend where I can add a new product type...Well rather than going in and hardcoding in the new product type at all the various points, I am trying to streamline my system so that it just pulls the product types from the database for my switch statments or anywhere eles on the site where I need to access a product code or some other identifiying factor.
     
    absentx, Mar 6, 2011 IP
  7. eleetgeek

    eleetgeek Peon

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Did you read my reply?
    You are displaying and using the same logic.

    Use If Else rather switch.
     
    eleetgeek, Mar 7, 2011 IP
  8. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Okay okay...so something like this:

    
    while ($row = mysqli_fetch_array($data)) {
      
         if ($trigger == $row['name'])   {
              //do whatever I usually had the switch statment do
            }
              else{
                    //keep trying
                 }
    }
    
    
    PHP:
     
    absentx, Mar 7, 2011 IP
  9. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for the help guys...I have this all figured out.

    Basically when I first started this website I had all the different cases hard coded in...I eventually got around to making a database to hold the cases.

    Simple query and the if statement were all I eventually needed..again thanks for pointing me in the right direction.
     
    absentx, Mar 7, 2011 IP
  10. eleetgeek

    eleetgeek Peon

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Oh I told that earlier but, I was 'silently' expecting someone to come up with an extra-ordinary reply. Nevertheless, problem solved, right? :)
     
    eleetgeek, Mar 8, 2011 IP
  11. absentx

    absentx Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Yeah your way was the way to do it the whole time...and the problem is absolutely solved!

    Now I enter a new product type in the backend and I dont have to go change five different pages of stuff to get the system to work with the new product type..pretty nice actually.
     
    absentx, Mar 10, 2011 IP
  12. NLZ13

    NLZ13 Well-Known Member

    Messages:
    166
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    113
    #12
    Are you trying to
    switch($trigger) {
    case $dbresult1:
    case $dbresult2: ?
    Code (markup):
    Then why won't you just

    while ($obj = mysql_fetch_object($res)) {
       if ($trigger == $obj->fieldname) {
            echo "you made it?";
       }
    }
    Code (markup):
     
    NLZ13, Mar 10, 2011 IP