0

First, sorry for my English. I'm a beginner in these things, and i have a simple question. I'm storing data in mysql. One column contains data in the format (for example): a,b,c,d or a,c,d, or a,d ... Now, after query i want to change this values:

 a = apple, b=blueberry, c=cat, d=dog ...

for example if column contains

 a,d, 
i want to change and print apple, dog...

One more time, sorry for my English. Please ask if anone didn't get the question.

thanks.

2
  • Do you have such pre-defined array ? Commented Jan 28, 2014 at 12:58
  • You can use a "dictionary" for such purpose. SOmething with a key-value structure. The dictionary can be contained in the database or statically implemented inside your scripts. Commented Jan 28, 2014 at 13:00

5 Answers 5

2
$map = array('a' => 'apple', 'b'=>'blueberry', 'c'=>'cat', 'd'=>'dog');

$column = 'a,d'; //your data here

$result = strtr($column, $map);

update:

$arr = explode(',', $result);
$links = array();
foreach($arr as $item) { 
$links[] = $commonURLpart . $item; 
}
Sign up to request clarification or add additional context in comments.

2 Comments

It is almost perfect for me. One more thing missing. I want to create separated links from $result.
$arr = explode(',', $result); foreach($arr as $item) { $links[] = $commonURLpart . $item; }
1

I suggest you create another table like:

+--------+-----------+
| letter | word      |
+--------+-----------+
| a      | apple     |
| b      | blueberry |
| c      | cat       |
| d      | dog       |
+--------+-----------+

And then do a join query similar as this:

SELECT
    *
FROM
    `table1` as t1
JOIN
    `table2` as t2
ON
    t1.letter = t2.word

This will automatically convert your letter into desired words.

PS: I could make the query more understandable, if you could post an example of your original table data.

2 Comments

Your solution is same good for me, but not I am not fully understand. The column store data in this format " a,b,c,d "
How does the original table look like?
1

You can take advantage of the power of the REPLACE() string function: Usage:

REPLACE(str,from_str,to_str)

Returns the string str with all occurrences of the string from_str replaced by the string to_str. Also REPLACE() performs a case-sensitive match when searching for from_str.

URL: http://dev.mysql.com/doc/refman/5.1/en/string-functions.html

For example: SELECT REPLACE('www.mysql.com', 'w', 'Ww'); will output WwWwWw.mysql.com

Update: The method above changes the content during runtime at MySQL level. If this is not relevant, you can change the content of your output during runtime with simple PHP string replace magic, by using str_replace() PHP function. This way you will store the original data in database intact and you will output the desired substituted strings.

Comments

0

you can use if condition to do so, you need your custom values e.g

<?PHP
---prev code

$db_col     = strtolower($row['col']);
if($db_col=='a'){
   echo 'Apple';
}elseif($db_col=='b'){
    echo 'Blueberry'    ;
so on ........  
?>

Comments

0

I am assuming that you have the query result in an associative array...

so, first thing is "cycling" the array:

while($row = mysql_fetch_assoc($replace_with_name_of_associative_array))
{
   if (strtolower($row['repalace_with_column_name'])=='a'){ //strtolower just in case you have "A" and "a", "B" and "b",...
   echo 'Apple';
   }elseif(strtolower($row['repalace_with_column_name'])=='b'){
     echo 'Blueberry';
     }
   //and so on...

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.