douzhuo8312 2016-05-07 03:48
浏览 58
已采纳

使用PHP将多个SQL列组合到HTML表的单个列中

Is it possible to combine data from multiple columns in a MySQL table into a single column in an HTML table, where each record will be a new row in the HTML table?

In this example, a table in MySQL has two columns (Col1, Col2). The data from Col1 is displayed in the first column in the HTML table. The data from Col2 in MySQL is displayed in the second column in the HTML table. In another words, the HTML table matches the layout of the MySQL table.

<?php
$con = new mysqli('domain', 'username', 'password', 'database');
$sql = "select * from table1";
$sql_query = mysqli_query($con, $sql);

while ($row = mysqli_fetch_array($sql_query)) {
  $col1 = $row['col1'];
  $col2 = $row['col2'];

  echo "<table>";
  echo "<tr>";
  echo "<td> $col1 </td>";
  echo "<td> $col2 </td>";
  echo "</tr>";
  echo "</table>";
}
?>

MySQL Table:

| - - - - | - - - - - - - |
| Col1    |    Col2       |
| - - - - | - - - - - - - |
| Blue    |    Car        |
| Green   |    Truck      |
| Yellow  |    Van        |
| - - - - | - - - - - - - |

HTML Table:

| - - - - | - - - - - - - |
| Column1 |    Column2    |
| - - - - | - - - - - - - |
| Blue    |    Car        |
| Green   |    Truck      |
| Yellow  |    Van        |
| - - - - | - - - - - - - |

If $col1 and $col2 are put inside of a single TD tag, this does get both $col1 and $col2 to display in Col1 in the HTML table. However, both $col1 and $col2 are displayed in the same cell.

<?php
$con = new mysqli('domain', 'username', 'password', 'database');
$sql = "select * from table1";
$sql_query = mysqli_query($con, $sql);

while ($row = mysqli_fetch_array($sql_query)) {
  $col1 = $row['col1'];
  $col2 = $row['col2'];

  echo "<table>";
  echo "<tr>";
  echo "<td> $col1 $col2 </td>";
  echo "</tr>";
  echo "</table>";
}
?>

HTML Table:

| - - - - - - - - - - - - |
| Column1                 |
| - - - - - - - - - - - - |
| Blue Car                |
| Green Truck             |
| Yellow Van              |
| - - - - - - - - - - - - |

Is it possible to echo $Col1 and $Col2 in Column1 of the HTML table and have each record be in its own row in the HTML table?

| - - - - - - - - - - - - |
| Column1                 |
| - - - - - - - - - - - - |
| Blue                    |
| Green                   |
| Yellow                  |
| Car                     |
| Truck                   |
| Van                     |
| - - - - - - - - - - - - |
  • 写回答

2条回答 默认 最新

  • duanjianxu4288 2016-05-07 03:55
    关注

    No need to have two loops. It can be done in single loop. Separate the col1 and col2 values in different variables. and at the end print it.

    <?php
    $con = new mysqli('domain', 'username', 'password', 'database');
    $sql = "select * from table1";
    $sql_query = mysqli_query($con, $sql);
    $str = "<table>";
    $str_col2 = "";
    while ($row = mysqli_fetch_array($sql_query)) {
      $col1 = $row['col1'];
      $col2 = $row['col2'];    
    
      $str .= "<tr><td> $col1 </td></tr>";
      $str_col2 .= "<tr><td> $col2 </td></tr>";
    }
    echo $str . $str_col2 . "</table>";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算