Clarkeez Posted July 3, 2011 Share Posted July 3, 2011 <?php if($data['cclass'] == "Warrior") { return '<span style="color:#C79C6E;">'.$input.'</span>'; } elseif($data['cclass'] == "Paladin") { return '<span style="color:#F58CBA;">'.$input.'</span>'; } elseif($data['cclass'] == "Hunter") { return '<span style="color:#ABD473;">'.$input.'</span>'; } elseif($data['cclass'] == "Rogue") { return '<span style="color:rgb(255,245,105);">'.$input.'</span>'; } elseif($data['cclass'] == "Priest") { return '<span style="color:#FFFFFF;">'.$input.'</span>'; } elseif($data['cclass'] == "Death Knight") { return '<span style="color:#C41F3B;">'.$input.'</span>'; } elseif($data['cclass'] == "Shaman") { return '<span style="color:#0070DE;">'.$input.'</span>'; } elseif($data['cclass'] == "Mage") { return '<span style="color:#69CCF0;">'.$input.'</span>'; } elseif($data['cclass'] == "Warlock") { return '<span style="color:#9482C9;">'.$input.'</span>'; } elseif($data['cclass'] == "Druid") { return '<span style="color:#FF7D0A;">'.$input.'</span>'; } ?> I used to be able to majorly simplify this kind of thing but ive forgotten, any help would be v nice Quote Link to comment https://forums.phpfreaks.com/topic/241001-how-can-i-simplify-something-like-this/ Share on other sites More sharing options...
EdwinPaul Posted July 3, 2011 Share Posted July 3, 2011 <?php if($data['cclass'] == "Warrior") { return '<span style="color:#C79C6E;">'.$input.'</span>'; } elseif($data['cclass'] == "Paladin") { return '<span style="color:#F58CBA;">'.$input.'</span>'; } elseif($data['cclass'] == "Hunter") { return '<span style="color:#ABD473;">'.$input.'</span>'; } elseif($data['cclass'] == "Rogue") { return '<span style="color:rgb(255,245,105);">'.$input.'</span>'; } elseif($data['cclass'] == "Priest") { return '<span style="color:#FFFFFF;">'.$input.'</span>'; } elseif($data['cclass'] == "Death Knight") { return '<span style="color:#C41F3B;">'.$input.'</span>'; } elseif($data['cclass'] == "Shaman") { return '<span style="color:#0070DE;">'.$input.'</span>'; } elseif($data['cclass'] == "Mage") { return '<span style="color:#69CCF0;">'.$input.'</span>'; } elseif($data['cclass'] == "Warlock") { return '<span style="color:#9482C9;">'.$input.'</span>'; } elseif($data['cclass'] == "Druid") { return '<span style="color:#FF7D0A;">'.$input.'</span>'; } ?> I used to be able to majorly simplify this kind of thing but ive forgotten, any help would be v nice Maybe this is usefull ? http://php.net/manual/en/control-structures.switch.php Quote Link to comment https://forums.phpfreaks.com/topic/241001-how-can-i-simplify-something-like-this/#findComment-1237915 Share on other sites More sharing options...
PFMaBiSmAd Posted July 3, 2011 Share Posted July 3, 2011 You would use an array with the key/value pairs, so that you don't need to alter your program logic every time you add or change a value. Quote Link to comment https://forums.phpfreaks.com/topic/241001-how-can-i-simplify-something-like-this/#findComment-1237917 Share on other sites More sharing options...
Pikachu2000 Posted July 3, 2011 Share Posted July 3, 2011 EDIT: Basically the same as what PFMaBiSmAd said ^^^ Storing the values in an array, and using the array element's value to fill in the color: attribute would reduce the amount of code, if that's your goal. $classes = array( 'Warrior' => '#C79C6E', 'Paladin' => '#F58CBA', 'Hunter' => '#ABD473'); // Etcetera. return "<span style=\"color:{$classes[$data['cclass']]};\">$input</span>"; Quote Link to comment https://forums.phpfreaks.com/topic/241001-how-can-i-simplify-something-like-this/#findComment-1237921 Share on other sites More sharing options...
Clarkeez Posted July 3, 2011 Author Share Posted July 3, 2011 Ahh guys, thankyou very much.. I did try the switch method but turned out just as bulky as individual if statements.. The array is what I was looking for Thanks again all who replied for your effort <?php $classes = array( 'Warrior' => '#C79C6E', 'Paladin' => '#F58CBA', 'Hunter' => '#ABD473', 'Rogue' => 'rgb(255,245,105)', 'Priest' => '#FFFFFF', 'Death Knight' => '#C41F3B', 'Shaman' => '#0070DE', 'Mage' => '#69CCF0', 'Warlock' => '#9482C9', 'Druid' => '#FF7D0A'); return "<span style=\"color:{$classes[$data['cclass']]};\">$input</span>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/241001-how-can-i-simplify-something-like-this/#findComment-1237925 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.