dongmei8071 2017-08-14 06:51
浏览 46
已采纳

如何在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.

  • 写回答

2条回答 默认 最新

  • dongshendie8849 2017-08-14 07:43
    关注

    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"]
    
     ];}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效