Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Switch to return a name instead of number

    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:

    PHP Code:
    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!

  2. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,141
    Thanks
    10
    Thanked 1,163 Times in 1,154 Posts
    What is happening now ... with the script you have?

  3. #3
    New Coder
    Join Date
    Nov 2011
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I have a table in my database (users) that contains a field (class) that is comprised of numbers. I want to convert those numbers to class names (warrior, mage, etc.) and have it print out what class name that person is, rather than just "You are a 1."

    Does that makes sense?

  4. #4
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,141
    Thanks
    10
    Thanked 1,163 Times in 1,154 Posts
    example with a few names.
    0=none ... since arrays start at zero (0) ...

    $names=array("none", "Alchemist", "Assassin", "Dark Arts", "Dark Paladin");
    echo "Name is: ".$names[$class];

  5. #5
    New Coder
    Join Date
    Nov 2011
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hmm... I got this error:

    Warning: mysql_result() [function.mysql-result]: None not found in MySQL result index 615 in

    Here is what I have.
    PHP Code:
    function getClassByNumber($x){
    switch(
    $x){
    case 
    0: return "None";
    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";
    }
    }

    $names=array("none""Alchemist""Assassin""Dark Arts""Dark Paladin""Entertainer""Hunter""Mage""Monk""Paladin""Pirate""Priest""Psion""Scholar""Thief""Warlock""Warrior");

    $dString "The $names $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"

  6. #6
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,141
    Thanks
    10
    Thanked 1,163 Times in 1,154 Posts
    That error must be from a different part of the script.
    I see nothing in what you posted that indicates any MySQL functions or the $names array.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •