如何在while循环中获取列的所有值并将这些值放入数组中
Below I have a table that displays the data from MySQL, in the database, there's three column for storing different languages of msg (ie traditionalmessage, simplifiedmessage &engmessage).
Each individual can pick their preferred language, in the view page
, there's an msg column
, it will show people's msg with their preferred languages
Like this
People Language Msg
A 繁體 物理治療
B Eng Physiotherapy
<table>
<?php
$result = mysql_query("SELECT *
FROM treatmentdetail WHERE
language ='繁體' OR language ='ENG' OR language ='简体'");?>
<?php
$specific = [];
while($row = mysql_fetch_array( $result ,MYSQL_ASSOC)) {?>
<tr>
<td><input class="form-control" type="text" size="1" name="people[]"
value="<?php echo $row['people'] ?>"></td>
<td><input class="form-control" type="text" size="1" name="language[]"
value="<?php echo $row['language'] ?>"></td>
<td width="200px"><input class="form-control" type="text" size="1"
name="msg[]" value="
<?php if($row['language'] == '繁體')
{
echo $row['traditionalmessage'];}
else if($row['language'] == 'ENG')
{
echo $row['engmessage'];}
if($row['language'] == '简体')
{
echo $row['simplifiedmessage'];} ?>">
</td>
Close the table
<?php
echo "</tr>";
echo "</table>";echo "</form>";?>
Now I'd like to get all the values of the msg column
and put them in an array called $specific
<?php $specific = [
"message" => $row["engmessage"]
];?>
Here I just hard coded to get the values of all message. But as there are gonna be traditionalmessage & simplifiedmessage as well. So it has to be dynamic.
So how can I get the all the values of the msg column (which may have values retrieved from multiple database columns
i.e. traditionalmessage/
simplifiedmessage /engmessage)
and put those values into an array
Hope u understand what I meant.
Many thanks.
I can get my desired result with the use of the if else statement. I am aware that there's certainly better answer out there. IF u happen to have one, please share . Thanks again to those who have helped along the way.
if($row['language'] == '繁體')
{
$specific[] = [
"message" => $row["traditionalmessage"]
];}
else if($row['language'] == '简体')
{
$specific[] = [
"message" => $row["simplifiedmessage"]
];}
else if($row['language'] == 'ENG')
{
$specific[] = [
"message" => $row["engmessage"]
];}
What problem? As I understand you need to extract all columns with message and loop by them. You know all column names or you need to determine it dynamically? If you know it before, you can loop over $row
array and extract needed values into an array.
foreach ($row as $key) {
//ignore columns with no 'message' in title
if (FALSE === strpos($key, 'message')) continue;
// do with $row[$key] what you want
}
As respected member Strawberry said you need to use mysqli (mysql improved or pdo, or mysqlnd) extension to work with DB instead of deprecated and obsolete mysql extension.
General advice You table structure is wrong if you need to add new columns for each new language. You need to rethink you table structure. Google about 'db normalisation'.