zelig Posted November 28, 2011 Share Posted November 28, 2011 Here is my dilemma. I'm trying to get the "class name" to return instead of the number that is associated with it. I wrote a switch so that each of the 16 cases would be associated with what the class name is. However, how do I make it output into my statement? Here is what I have so far: function getClassByNumber($x){ switch($x){ case 1: return "Alchemist"; case 2: return "Assassin"; case 3: return "Dark Arts"; case 4: return "Dark Paladin"; case 5: return "Entertainer"; case 6: return "Hunter"; case 7: return "Mage"; case 8: return "Monk"; case 9: return "Paladin"; case 10: return "Pirate"; case 11: return "Priest"; case 12: return "Psion"; case 13: return "Scholar"; case 14: return "Thief"; case 15: return "Warlock"; case 16: return "Warrior"; } } $class = getClassByNumber($username->class); echo You are now a $class. Username is defined as seeking the table I want. Class is the field where the number is stored. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/251926-help-with-a-switch/ Share on other sites More sharing options...
teynon Posted November 28, 2011 Share Posted November 28, 2011 Here is my dilemma. I'm trying to get the "class name" to return instead of the number that is associated with it. I wrote a switch so that each of the 16 cases would be associated with what the class name is. However, how do I make it output into my statement? Here is what I have so far: function getClassByNumber($x){ switch($x){ case 1: return "Alchemist"; case 2: return "Assassin"; case 3: return "Dark Arts"; case 4: return "Dark Paladin"; case 5: return "Entertainer"; case 6: return "Hunter"; case 7: return "Mage"; case 8: return "Monk"; case 9: return "Paladin"; case 10: return "Pirate"; case 11: return "Priest"; case 12: return "Psion"; case 13: return "Scholar"; case 14: return "Thief"; case 15: return "Warlock"; case 16: return "Warrior"; } } $class = getClassByNumber($username->class); echo You are now a $class. Username is defined as seeking the table I want. Class is the field where the number is stored. Thanks! echo "You are now a $class."; Quote Link to comment https://forums.phpfreaks.com/topic/251926-help-with-a-switch/#findComment-1291719 Share on other sites More sharing options...
zelig Posted November 28, 2011 Author Share Posted November 28, 2011 Hmm... It comes up as a "None" result, rather than the appropriate one. Quote Link to comment https://forums.phpfreaks.com/topic/251926-help-with-a-switch/#findComment-1291722 Share on other sites More sharing options...
Pikachu2000 Posted November 28, 2011 Share Posted November 28, 2011 You could also use a "lookup" array. $class = array( 1 => "Alchemist", "Assassin", "Dark Arts", "Dark Paladin", "Entertainer", "Hunter", "Mage", "Monk", "Paladin", "Pirate", "Priest", "Psion", "Scholar", "Thief", "Warlock", "Warrior" ); echo "You are now a " . $class[$username->class]; Quote Link to comment https://forums.phpfreaks.com/topic/251926-help-with-a-switch/#findComment-1291724 Share on other sites More sharing options...
zelig Posted November 28, 2011 Author Share Posted November 28, 2011 I can't seem to get the lookup array to work with what I'm doing. Here is the statement that I'm using instead of just the plain echo. $dString = "The $class $peepr known as $button<a href=main.php?username=$username&password=$password&action=look+at+$peeps[$i] $overlib>$thepeepname</a> is here$d<br>\n"; I don't know how to add the array lookup where $class is and still have it work correctly... Quote Link to comment https://forums.phpfreaks.com/topic/251926-help-with-a-switch/#findComment-1291726 Share on other sites More sharing options...
Pikachu2000 Posted November 28, 2011 Share Posted November 28, 2011 Assuming it's $username->class like in your post above, using {complex notation} should do it. $dString = "The {$class[$username->class]} $peepr known as $button<a href=main.php?username=$username&password=$password&action=look+at+$peeps[$i] $overlib>$thepeepname</a> is here$d<br>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/251926-help-with-a-switch/#findComment-1291729 Share on other sites More sharing options...
zelig Posted November 28, 2011 Author Share Posted November 28, 2011 Hmm... that helped with the error showing up in regards to the statement, but there is still nothing showing up. Quote Link to comment https://forums.phpfreaks.com/topic/251926-help-with-a-switch/#findComment-1291732 Share on other sites More sharing options...
Pikachu2000 Posted November 28, 2011 Share Posted November 28, 2011 What error? Quote Link to comment https://forums.phpfreaks.com/topic/251926-help-with-a-switch/#findComment-1291733 Share on other sites More sharing options...
zelig Posted November 28, 2011 Author Share Posted November 28, 2011 It just didn't like the quotation marks. Quote Link to comment https://forums.phpfreaks.com/topic/251926-help-with-a-switch/#findComment-1291735 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.