Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-28-2011, 02:31 AM   PM User | #1
zelig
New Coder

 
Join Date: Nov 2011
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
zelig is an unknown quantity at this point
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!
zelig is offline   Reply With Quote
Old 11-28-2011, 03:20 AM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
What is happening now ... with the script you have?
mlseim is offline   Reply With Quote
Old 11-28-2011, 03:23 AM   PM User | #3
zelig
New Coder

 
Join Date: Nov 2011
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
zelig is an unknown quantity at this point
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?
zelig is offline   Reply With Quote
Old 11-28-2011, 03:48 AM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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];
mlseim is offline   Reply With Quote
Old 11-28-2011, 04:11 AM   PM User | #5
zelig
New Coder

 
Join Date: Nov 2011
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
zelig is an unknown quantity at this point
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"
zelig is offline   Reply With Quote
Old 11-28-2011, 12:16 PM   PM User | #6
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:27 AM.


Advertisement
Log in to turn off these ads.