Need help creating a Switch case

Discussion in 'PHP' started by WeX09, Sep 29, 2011.

  1. #1
    I have the following code:

    $model = $row['v_products_model'];
    $smallid = substr($model, 0, 4);

    There are 123 values for $smallid.

    I need to give a value to the variable $mpg based on the value of $smallid, like this:

    If $smallid = '5456' then $mpg = 'Mouse'
    If $smallid = '5434' then $mpg = 'Speakers'
    If $smallid = '5432' then $mpg = 'Cable'
    If $smallid = '5431' then $mpg = 'Adapters'

    etc, etc.

    I need someone help me in building the code. I'm not sure if switch is the answer or may be another implementation.

    Please help. I'm in a hurry with this.

    Thanks
     
    Solved! View solution.
    WeX09, Sep 29, 2011 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You could use an array pretty easily.

    
    $mpg  = array(
        5456 => 'Mouse',
        5434 => 'Speakers',
        5432 => 'Cable',
        5431 => 'Adapters'
    );
    
    echo $mpg[$smallid];
    
    
    PHP:
    You would need to create the array, but shouldn't be too difficult.
     
    jestep, Sep 29, 2011 IP
  3. WeX09

    WeX09 Member

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    And how can I do it? - I need help in doing that. Thank you for posting
     
    WeX09, Sep 29, 2011 IP
  4. #4
    As you said there are 123 values for $smallid. I hope these values are fixed.

    Create one associative array for all 123 values like following:

    $mpqArr = array(

    5456 => 'Mouse',
    5434 => 'Speakers',
    5432 => 'Cable',
    5431 => 'Adapters'
    put all reamining also here as similar way
    );


    $model = $row['v_products_model'];

    $smallid = substr($model, 0, 4);

    $mpg = $mpgArr[$smallid];

    $mpg will have the exact value which you wanted.



    I hope you will able to write code now.



    Sheetal
     
    SheetalCreation, Sep 29, 2011 IP
  5. vruvishal

    vruvishal Member

    Messages:
    22
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #5
    Hi,

    switch( $task )
    {
    case "5456":
    Mouse.php
    break;
    case "5434":
    Speakers.php
    break;

    case "5432":
    Cable.php
    break;
    case "5431":
    Adapters.php
    break;

    }


    Vishal
     
    vruvishal, Sep 29, 2011 IP
    Raj Kumar likes this.
  6. vruvishal

    vruvishal Member

    Messages:
    22
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #6
    Hi,

    switch( $smallid )
    {
    case "5456":
    Mouse.php
    break;
    case "5434":
    Speakers.php
    break;

    case "5432":
    Cable.php
    break;
    case "5431":
    Adapters.php
    break;

    }


    Vishal
     
    vruvishal, Sep 29, 2011 IP
  7. WeX09

    WeX09 Member

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    Thank you sheetal. I was able with your help to complete the code. Thanks!
     
    WeX09, Sep 30, 2011 IP