homepage Welcome to WebmasterWorld Guest from 107.20.25.215
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Too many if() statements - this feels wrong
jake66




msg:3779658
 3:29 am on Nov 4, 2008 (gmt 0)

This sure looks like a lot of if() statements, but it does exactly what I want it to do.

Is this too much, or is there a way to condense it?

if (isset($HTTP_GET_VARS['category'])){
echo'You may also like <a href="' . tep_href_link(SIMILAR) . '">these</a>';
}else{
if (isset($HTTP_GET_VARS['manid']) && $themanquer['similar_country'] !==''){
if ($themanquer['similar_country'] =='CAN'){
$location='Canadian';
}elseif ($themanquer['similar_country'] =='FIN'){
$location='Finnish';
}elseif ($themanquer['similar_country'] =='GER'){
$location='German';
}elseif ($themanquer['similar_country'] =='NOR'){
$location='Norwegian';
}elseif ($themanquer['similar_country'] =='SWE'){
$location='Swedish';
}elseif ($themanquer['similar_country'] =='USA'){
$location='American';
}
echo'Can\'t find it? Try more <a href="' . tep_href_link('loc-'.$themanquer['similar_country']) . '-' . $themanquer['similar_type'].'">'.$location.' ' .$themanquer['similar_type'].'</a>, ';
echo'and <a href="/type-'.$themanquer['similar_type'].'">'.$themanquer['similar_type'],'</a><br />';
}else{
echo'Try other <a href="/type-'.$themanquer['similar_type'].'">'.$themanquer['similar_type'],'</a> stuff.<br/>';
}
}

 

jatar_k




msg:3779680
 4:28 am on Nov 4, 2008 (gmt 0)

you could try

switch() [php.net]

adb64




msg:3779734
 7:13 am on Nov 4, 2008 (gmt 0)

Or try this

Define an array somewhere at the top of your PHP file:

$Countries = array(
'CAN' => 'Canadian',
'FIN' => 'Finnish',
'GER' => 'German',
'NOR' => 'Norwegian',
'SWE' => 'Swedish',
'USA' => 'American');

And where you now have all the if() statements:

if (isset($Countries[$themanquer['similar_country']]))
{
$location = $Countries[$themanquer['similar_country']];
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved