[英]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.