Page 1 of 1
Function convert value like "0" to "text" from mySQL field
Posted: Wed Mar 11, 2009 11:26 pm
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

Re: Function convert value like "0" to "text" from mySQL field
Posted: Thu Mar 12, 2009 12:43 am
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);
Re: Function convert value like "0" to "text" from mySQL field
Posted: Mon Mar 16, 2009 7:00 pm
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.
Re: Function convert value like "0" to "text" from mySQL field
Posted: Mon Mar 16, 2009 7:01 pm
by semlar
Code: Select all
echo "<td>" . return_clarity($row['clarity_grade']) . "</td>";
?
Re: Function convert value like "0" to "text" from mySQL field
Posted: Mon Mar 16, 2009 7:02 pm
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]
Re: Function convert value like "0" to "text" from mySQL field
Posted: Tue Mar 17, 2009 12:18 am
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!