Jump to content

Translate numbers from a database into words


LordPsyan

Recommended Posts

OK, this should be pretty simple, but I can't figure it out. I have a field in my database called type. This is a type of armor. I can grab the information from the database ORDER by type ASC. now my table prints out the items fine, and it will show the item type number, but I want it to say the item type not the item type number. the item type is not in the database. here is an example:

type number - item type:

0 - Other

1 - Head

2 - Neck

3 - Shoulder

4 - Shirt

The field has 0-4 depending on what type of item it is. so when I tell my code to print out the number, and order it by that number, it shows the number fine.

 

What I would really like to do, is have it print 1 time the item type. for example, it would print:

 

Head

helm

cap

bandana

 

neck

necklace

choker

 

shoulder

shoulderpads of strength

shoulderpads of agility

 

and so on.

My actual code is a simple while loop:

 

$query="select * from items ORDER BY Type ASC";
$rt=mysql_query($query);
echo mysql_error();
while($nt=mysql_fetch_array($rt)){
echo "$nt[type <br /> $nt[entry]";
}

this works, and prints out all other first, then head and so on (my code is a little more pertified than this, but I shortened it only showing the important part.)

 

I can answer any more questions about this if more info is needed.

 

Thank you,

 

LP

Link to comment
Share on other sites

the item type is not in the database.

 

It should be. You should have a separate table for the items(indeed, a separate table for potential items and a table for items that users have)

 

Without storing the item type in the database, you would have a situation where you have a whole stack of if/switch statements to work out what the item is. You would then have to do that each time you wanted to find the item. Then, what happens when you wish to add an extra item?

 

You probably want something like this:

 

table name: item_types

id (auto increment field)

type

 

table name: items:

id(auto increment field)

type_id(link with item_types.id

name

//i would assume other fields such as power, defence etc

 

table name: user_items

id(auto increment field)

user_id (link with users.id)

item_id (link with items.id)

 

You'll then be in a situation to join your tables in yoru queries to extract the relevant information.

Link to comment
Share on other sites

I wish I could change the structure of the database. however, the database is used elsewhere, and cannot be changed. So I really don't mind a bunch of if/switches. I only have to write this page once, and for the most part, I can copy paste most of it. only have to modify a few small things. I am just not that good with if statements.

 

I would assume something similar to

 

if ('type' == 1){
echo "head';} else{
if ('type' == 2)
echo "shoulder";}
else {
//finish up the script.

but I need to know the exact syntax. I know nothing about swiches, cept that it would replace all those if's I think.

 

Thanks,

 

LP

Link to comment
Share on other sites

Re-reading your post, i think what you're after is something like:

 

<?php
$types = array('Other','Head','Neck','Shoulder','Shirt');//define the types - the key of the array refers to the type number
$sql = "SELECT type,entry FROM items ORDER BY type ASC";
$result = mysql_query($sql) or die(mysql_error());
$prev_type = '';
while(list($type,$entry) = mysql_fetch_row($sql)){
    if($type != $prev_type){
        echo $types[$type].'<br />';
        $prev_type = $type;
    }
    echo $entry.'<br />';
}
?>

Link to comment
Share on other sites

that looks about right, however it prints out nothing. I even put "haha" in the echo statement to see if it is grabbing anything, and it isn't. haha isnt printed anywhere on the screen. I did notice I made an error. it "type" the table is "InventoryType". I tried multiple things, such as replacing anywhere that said "type" to InventoryType" but for some reason, nothing prints.

 

Thanks.

Link to comment
Share on other sites

Well, there's no parse errors in the code i've provided and if there's an error with the mysql, then that should be echoed. Perhaps there's something else in the script causing the problem?

Try adding these two lines to the top of your file:

error_reporting(E_ALL);
ini_set('display_errors','On');

 

And see if there is now an error shown.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.