简体   繁体   English

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

[英]Combine multiple SQL columns into a single column of an HTML table using PHP

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? 是否可以将来自MySQL表的多个列的数据合并到HTML表的单个列中,其中每个记录将是HTML表中的新行?

In this example, a table in MySQL has two columns (Col1, Col2). 在此示例中,MySQL中的表具有两列(Col1,Col2)。 The data from Col1 is displayed in the first column in the HTML table. 来自Col1的数据显示在HTML表的第一列中。 The data from Col2 in MySQL is displayed in the second column in the HTML table. 来自MySQL中Col2的数据显示在HTML表的第二列中。 In another words, the HTML table matches the layout of the MySQL table. 换句话说,HTML表与MySQL表的布局匹配。

<?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: MySQL表:

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

HTML Table: HTML表格:

| - - - - | - - - - - - - |
| 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. 如果将$ col1和$ col2放在单个TD标签中,则确实会将$ col1和$ col2都显示在HTML表的Col1中。 However, both $col1 and $col2 are displayed in the same cell. 但是,$ col1和$ col2都显示在同一单元格中。

<?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: HTML表格:

| - - - - - - - - - - - - |
| 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? 是否可以在HTML表的Column1中回显$ Col1和$ Col2,并使每个记录都位于HTML表的自己的行中?

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

Your HTML is valid, but not correct. 您的HTML有效,但不正确。 This is might be the structure of your resultant table. 这可能是结果表的结构。

Make a new array where you store the col2 value for showing after all the col1 value. 创建一个新数组,在其中存储col2值,以在所有col1值之后显示。 After completing the col1 value loop again for the second col2 values for the display. 完成col1值循环后,再次显示第二个col2值。

$col2 = array();
echo "<table>";
while ($row = mysqli_fetch_array($sql_query)) {
  $col1 = $row['col1'];
  $col2[] = $row['col2'];

  echo "<tr>";
  echo "<td> $col1 </td>";
  echo "</tr>";
}
for($i = 0; $ < count($col2); $i++){
  echo "<tr>";
  echo "<td> $col2[$i] </td>";
  echo "</tr>";
}
echo "</table>";

No need to have two loops. 不需要有两个循环。 It can be done in single loop. 可以在单循环中完成。 Separate the col1 and col2 values in different variables. 用不同的变量分隔col1和col2值。 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>";
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 PHP 将单个 SQL 列显示到 HTML 表的多列中 - Display a single SQL columns into multiple columns of an HTML table using PHP PHP - SQL:将两行合并为多列的单行 - PHP - SQL: Combine two rows into single row with multiple columns php/html中多列显示单列mysql数据 - Display single column mysql data in multiple columns in php/html 为每个唯一ID获取mysql表的多列,并使用PHP为每个唯一ID在HTML表的单行中回显结果 - fetching multiple columns of mysql table for each unique id and echoing the result in a single row of a HTML table for each unique id using PHP 如何在php SQL中将多列合并为一列 - How to Combine multiple column into one in php SQL 使用PHP将SQL Join语句放入HTML表中的单个字段 - SQL Join statement into a single field in a HTML Table using PHP MySQL - 如何在多列html表中显示mysql表的单列 - MySQL - How to show in multiple columns html table, single column of mysql table PHP MySQL在单个表中计算多个列 - PHP MySQL Counting Multiple Columns in single table 使用带有SQL的JQuery PHP向html表中添加作为方程式的列 - adding a column which is an equation to a html table using JQuery PHP with SQL 在SQL select语句中将多个列值合并为单个值 - Combine multiple column values in to single value in SQL select statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM