Log in

View Full Version : Resolved PHP - Else / IF to Switch



wackyedd
06-26-2012, 10:55 AM
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
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

Nile
06-26-2012, 11:56 AM
<?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;
?>

wackyedd
06-26-2012, 11:58 AM
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

Nile
06-26-2012, 12:05 PM
No problem, I'm glad to help :D

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"

wackyedd
06-26-2012, 12:07 PM
Resolved as suggested sir, you have my thanks!

Kind regards and keep on coding!

Edd