SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    switch() multiple cases

    hi all

    Is it possible to convert the below code to switch statement.

    PHP Code:
    <?
    if(($category == 20) && ($dealer == 25))
        {
        
    $qry="select * from brands where gender='men'";
        }
        
    if((
    $category == 20) && ($dealer == 26))
        {
        
    $qry="select * from brands where gender='women'";
        }    
    ?>
    vineet

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,491
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    You could do this
    PHP Code:
    switch (true) {
      case ((
    $category == 20) && ($dealer == 25)): 
        
    $qry="select * from brands where gender='men'"
        break;
      case ((
    $category == 20) && ($dealer == 26)): 
        
    $qry="select * from brands where gender='women'"
        break;

    But I don't know if it is more readable and understandable?

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Are there potentially many more of these conditions to cater for? If so, how many and what do some of them look like?

  4. #4
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    You could do this
    PHP Code:
    switch (true) {
      case ((
    $category == 20) && ($dealer == 25)): 
        
    $qry="select * from brands where gender='men'"
        break;
      case ((
    $category == 20) && ($dealer == 26)): 
        
    $qry="select * from brands where gender='women'"
        break;

    But I don't know if it is more readable and understandable?
    thanks guido

    i didnt knew it was possible.

    I was only having knowledge about the standard switch statement with 1 case

    vineet

  5. #5
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Are there potentially many more of these conditions to cater for? If so, how many and what do some of them look like?
    hi cups

    at present there are only 4 similar cases.

    vineet

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    switch (true) {
      case ((
    $category == 20) && ($dealer == 25)): 
        
    $where " gender ='men'";
        break;
      case ((
    $category == 20) && ($dealer == 26)): 
        
    $where "gender = 'women'"
        break;
    }

    $query "select * from brands where " $where

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,031
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Does it have to be switch ?

    PHP Code:
    $where = array(
       
    20 => array(
          
    25 => 'gender="men"',
          
    26 => 'gender="women"',
       )
    );

    if (isset(
    $where[$category][$dealer]))
    {
        
    $query "select * from brands where ".$where[$category][$dealer];
    }
    else
    {
        throw new \
    Exception("Unknown category/dealer combination (`$category`, `$dealer`)");

    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •