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条)

报告相同问题?

悬赏问题

  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示