Function convert value like "0" to "text" from mySQL field

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NiceIce
Forum Newbie
Posts: 3
Joined: Wed Mar 11, 2009 11:03 pm

Function convert value like "0" to "text" from mySQL field

Post by NiceIce »

I'm very new to php and have been trying to solve this for days, searching for an example on Google, etc. so hopefully somebody can point me in the right direction...

I need to convert numerical values from a field [clarity_grade] within a mySQL table to a text value, the data is imported to the mySQL server from a script provided by one of my suppliers, the data comes from them in numerical format.

The script they provided comes with this example of how to convert the data:

function return_clarity($input){
$clarity_grade_array = array(
0 => "IF",
1 => "VVS1",
2 => "VVS2",
3 => "VS1",
4 => "VS2",
5 => "SI1",
6 => "SI2",
7 => "I1",
8 => "I2",
9 => "I3");
return $clarity_grade_array[$input];
}

Unfortunately the programmer they had who wrote the script is gone, so I can't ask him for advice / help.

I've tried replacing $input with $clarity_grade because I believe the $input reference is intended for use with a form based query and I want to display the data in a table.

When I bring the data in from the mySQL table, it still displays as a number. I've tried calling it as &clarity($clarity) and return_clarity($clarity) to no avail.

Somebody please put me out of my misery :banghead:
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: Function convert value like "0" to "text" from mySQL field

Post by semlar »

I don't understand what you're trying to do.

This function takes the number you give it and returns the value associated with the array key.

return_clarity(6) returns SI2.

Just feed whatever variable you're pulling out of the table as a number into the function, I don't know what you're calling the variable.

$var = 6;
echo return_clarity($var);
NiceIce
Forum Newbie
Posts: 3
Joined: Wed Mar 11, 2009 11:03 pm

Re: Function convert value like "0" to "text" from mySQL field

Post by NiceIce »

I think the problem exists because I can't call the data as you've suggested, like this:

$var = 6;
echo return_clarity($var);

Because it's stored in mySQL in numerical format, so I need a function to convert the numerical data to the defined text as dictated in the function in my original post, on import into the table like this:

$result = mysql_query("SELECT * FROM inventory");
$array[result] = $row;
{
echo "<tr>";
echo "<td>" . $row['shape_id'] . "</td>";
echo "<td>" . $row['weight'] . "</td>";
echo "<td>" . $row['clarity_grade'] . "</td>";
echo "<td>" . $row['color_grade'] . "</td>";
echo "<td>" . $row['fluorescence'] . "</td>";
echo "<td>" . $row['cut_grade'] . "</td>";

So "I think" that the problem is caused by how I'm calling the information into the table, so I need to know a different way to call the information to the table and display it by row while converting the numerical reference to the defined text.
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: Function convert value like "0" to "text" from mySQL field

Post by semlar »

Code: Select all

echo "<td>" . return_clarity($row['clarity_grade']) . "</td>";
?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Function convert value like "0" to "text" from mySQL field

Post by Benjamin »

Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]
NiceIce
Forum Newbie
Posts: 3
Joined: Wed Mar 11, 2009 11:03 pm

Re: Function convert value like "0" to "text" from mySQL field

Post by NiceIce »

Thanks semlar, that is exactly what I needed! I knew it had to be something simple and was hoping somebody with experience would be able to spot it!
Post Reply