KGodel |
05-02-2014 04:02 AM |
Code Optimization
So, I recently created a plugin to show icons on a user's postbit based on games they selected in several profile field multi-select drop down boxes. Below is my code, but I am wondering if there is an easier way to do it that someone can show me that would still be easy to update.
PHP Code:
// Get Basic Fields // $maingame = $post['field5']; $othergames = $post['field6']; $rpgs = $post['field41']; $shooters = $post['field42']; $strat = $post['field43']; $other = $post['field44']; // Set Games Output // $games = ""; //// Get Game Lists //// // Get Other Games $othergamelist = array(); $othergamelist[] = array("csgo","Counter Strike: Global Offensive"); $othergamelist[] = array("csgo","Counter Strike: Global Offensive"); $othergamelist[] = array("league_of_legends","League of Legends"); $othergamelist[] = array("league_of_legends","League of Legends"); $othergamelist[] = array("league_of_legends","League of Legends"); $othergamelist[] = array("minecraft","Minecraft"); $othergamelist[] = array("smite","Smite"); $othergamelist[] = array("tera","TERA"); // Get RPGs $rpglist = array(); $rpglist[] = array("aion","AION"); $rpglist[] = array("archage","Archage"); $rpglist[] = array("aura_kingdom","Arua Kingdom"); $rpglist[] = array("battlestar_galactica_online","Battlestar Galactica Online"); $rpglist[] = array("city_of_heroes","City of Heroes"); $rpglist[] = array("dc_universe_online","DC Universe Online"); $rpglist[] = array("defiance","Defiance"); $rpglist[] = array("dragons_prophet","Dragon's Prophet"); $rpglist[] = array("diablo_3","Diablo 3"); $rpglist[] = array("diablo_3_ros","Diablo 3: Reaper of Souls"); $rpglist[] = array("dnd_neverwinter","DND: Neverwinter"); $rpglist[] = array("eve_online","EVE Online"); $rpglist[] = array("guild_wars_2","Guild Wars 2"); $rpglist[] = array("maplestory","Maplestory"); $rpglist[] = array("path_of_exile","Path of Exile"); $rpglist[] = array("pokemmo","PokeMMO"); $rpglist[] = array("rift","RIFT"); $rpglist[] = array("runescape","Runescape"); $rpglist[] = array("star_wars_the_old_republic","Star Wars: The Old Republic"); $rpglist[] = array("vindictus","Vindictus"); $rpglist[] = array("war_thunder","War Thunder"); $rpglist[] = array("world_of_tanks","World of Tanks"); $rpglist[] = array("world_of_warcraft","World of Warcraft"); $rpglist[] = array("world_of_warplanes","World of Warplanes"); // Get Shooters // $shooterlist = array(); $shooterlist[] = array("apb","All Points Bulletin: Reloaded"); $shooterlist[] = array("battlefield","Battlefield Series"); $shooterlist[] = array("blr","Blacklight: Retribution"); $shooterlist[] = array("borderlands_2","Borderlands 2"); $shooterlist[] = array("call_of_duty","Call of Duty Series"); $shooterlist[] = array("combat_arms","Combat Arms"); $shooterlist[] = array("dayz","Day Z"); $shooterlist[] = array("firefall","Firefall"); $shooterlist[] = array("gta","Grand Theft Auto"); $shooterlist[] = array("gunz_2","Gunz 2: The Second Duel"); $shooterlist[] = array("half_life","Half Life Series"); $shooterlist[] = array("hawken","Hawken"); $shooterlist[] = array("infestation","Infestation: Survivor Stories"); $shooterlist[] = array("left_4_dead_2","Left 4 Dead 2"); $shooterlist[] = array("loadout","Loadout"); $shooterlist[] = array("planetside_2","Planetside 2"); $shooterlist[] = array("rust","Rust"); $shooterlist[] = array("team_fortress_2","Team Fortress 2"); $shooterlist[] = array("titanfall","Titanfall"); $shooterlist[] = array("uncharted","Uncharted Series"); $shooterlist[] = array("warface","Warface"); //Get Strategy// $stratlist = array(); $stratlist[] = array("civ_v","Civilization V"); $stratlist[] = array("dawngate","Dawngate"); $stratlist[] = array("dead_island","Dead Island: Epidemic"); $stratlist[] = array("dota_2","DOTA 2"); $stratlist[] = array("hearthstone","Hearthstone"); $stratlist[] = array("heroes_of_newerth","Heroes of Newerth"); $stratlist[] = array("heroes_of_the_storm","Heroes of the Storm"); $stratlist[] = array("infinite_crisis","Infinite Crisis"); $stratlist[] = array("ogame","OGame"); $stratlist[] = array("sim_city","Sim City"); $stratlist[] = array("starcraft_2","Starcraft 2"); //Get Others// $otherlist = array(); $otherlist[] = array("fifa","FIFA"); $otherlist[] = array("madden","Madden"); $otherlist[] = array("osu","OSU!"); $otherlist[] = array("starbound","Starbound"); $otherlist[] = array("street_fighter","Super Street Fighter 4");
// MAIN GAME // switch ($maingame) { case "Counter Strike: Global Offensive (EU)": $games .= "<img src='images/games/csgo.png' alt='Counter Strike: Global Offensive (EU)'/>"; break; case "Counter Strike: Global Offensive (NA)": $games .= "<img src='images/games/csgo.png' alt='Counter Strike: Global Offensive (NA)'/>"; break; case "League of Legends (EUNE)": $games .= "<img src='images/games/league_of_legends.png' alt='League of Legends (EUNE)'/>"; break; case "League of Legends (EUW)": $games .= "<img src='images/games/league_of_legends.png' alt='League of Legends (EUW)'/>"; break; case "League of Legends (NA)": $games .= "<img src='images/games/league_of_legends.png' alt='League of Legends (NA)'/>"; break; case "Minecraft": $games .= "<img src='images/games/minecraft.png' alt='Minecraft'/>"; break; case "Smite": $games .= "<img src='images/games/smite.png' alt='Smite'/>"; break; case "TERA (NA)": $games .= "<img src='images/games/tera.png' alt='TERA (NA)'/>"; break; case "Other Game": $games .= "<img src='images/games/other.png' alt='Other Game'/>"; break; }
// BASIC OTHER GAMES // $i = 0; foreach ($othergamelist as $value) { // Bitshift Operator works for Base 2 $bit = 1<<$i; $icon = $value[0]; $name = $value[1]; if ($othergames & $bit) { if (stristr($games,$name) === false) { $games .= "<img src='images/games/$icon" . ".png' alt='$name'/>"; } } // Move Bit Forward $i++; }
// RPG GAMES // $i = 0; foreach ($rpglist as $value) { // Bitshift Operator works for Base 2 $bit = 1<<$i; $icon = $value[0]; $name = $value[1]; if ($rpgs & $bit) { $games .= "<img src='images/games/$icon" . ".png' alt=\"$name\"/>"; } // Move Bit Forward $i++; } // SHOOTER GAMES // $i = 0; foreach ($shooterlist as $value) { // Bitshift Operator works for Base 2 $bit = 1<<$i; $icon = $value[0]; $name = $value[1]; if ($shooters & $bit) { $games .= "<img src='images/games/$icon" . ".png' alt='$name'/>"; } // Move Bit Forward $i++; } // STRATEGY GAMES // $i = 0; foreach ($stratlist as $value) { // Bitshift Operator works for Base 2 $bit = 1<<$i; $icon = $value[0]; $name = $value[1]; if ($strat & $bit) { $games .= "<img src='images/games/$icon" . ".png' alt='$name'/>"; } // Move Bit Forward $i++; } // OTHER GAMES // $i = 0; foreach ($otherlist as $value) { // Bitshift Operator works for Base 2 $bit = 1<<$i; $icon = $value[0]; $name = $value[1]; if ($other & $bit) { $games .= "<img src='images/games/$icon" . ".png' alt='$name'/>"; } // Move Bit Forward $i++; } // Display the Games // $template_hook['postbit_userinfo_right_after_posts'] .= "<dd> <fieldset style='border:1px solid #161616; padding:6px; margin:6px; clear:both;'> <legend style='font-size:12px;'>Games</legend> $games </fieldset> </dd>";
|