Results 1 to 5 of 5

Thread: PHP - Else / IF to Switch

  1. #1
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Arrow PHP - Else / IF to Switch

    Hello,

    I am looking for some assistance with my PHP code, I have created a simple database lookup and echo using else / if statements but I have been informed that it would be better to use a switch statement. How would i go about changing the code below to a switch?? I am lost.

    Here is the code:
    PHP Code:
                <?php 
                
    if(ICL_LANGUAGE_CODE=='de')
                {
                    
    $my_id 943;
                }
                else if(
    ICL_LANGUAGE_CODE=='nb')
                {
                    
    $my_id 944;
                }
                else if(
    ICL_LANGUAGE_CODE=='sv')
                {
                    
    $my_id 945;
                }
                else
                {
                    
    $my_id 909;
                }
                
    $post_id_7 get_post($my_id); 
                echo 
    $post_id_7->post_content;
                
    ?>
    I appreciate any tips or hints that can help me improve upon this!

    With thanks and kind regards

    Edd
    Last edited by wackyedd; 06-26-2012 at 12:08 PM. Reason: Thread Now Resolved Thanks To Nile!

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    PHP Code:
    <?php
    switch(ICL_LANGUAGE_CODE) {
        case 
    "de":
            
    $my_id 943;
            break;
        case 
    "nb":
            
    $my_id 944;
            break;
        case 
    "sv":
            
    $my_id 945;
            break;
        default:
            
    $my_id 909;
            break;
    }
    $post_id_7 get_post($my_id); 
    echo 
    $post_id_7->post_content;
    ?>

  3. The Following User Says Thank You to Nile For This Useful Post:

    wackyedd (06-26-2012)

  4. #3
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Nile,

    Thanks ever so much for your help, you did not need to write the code but I think I am starting to understand switch thanks to it.

    Your a star!

    Edd

  5. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    No problem, I'm glad to help

    In an effort to keep things organized, you have the option to set a thread to resolved when an issue is fixed. To make the status of the thread resolved:
    1. Go to your first post
    2. Edit your first post
    3. Click "Go Advanced"
    4. In the dropdown next to the title, select "RESOLVED"

  6. The Following User Says Thank You to Nile For This Useful Post:

    wackyedd (06-26-2012)

  7. #5
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Resolved as suggested sir, you have my thanks!

    Kind regards and keep on coding!

    Edd

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
  •