是否可以將來自MySQL表中多個列的數據組合到HTML表中的單個列中,其中每個記錄將成爲HTML中的新行表?使用PHP將多個SQL列組合到HTML表格的單個列中
在這個例子中,MySQL中的表有兩列(Col1,Col2)。來自Col1的數據顯示在HTML表格的第一列中。 MySQL中Col2的數據顯示在HTML表格的第二列中。換句話說,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表:
| - - - - | - - - - - - - |
| Col1 | Col2 |
| - - - - | - - - - - - - |
| Blue | Car |
| Green | Truck |
| Yellow | Van |
| - - - - | - - - - - - - |
HTML表:
| - - - - | - - - - - - - |
| Column1 | Column2 |
| - - - - | - - - - - - - |
| Blue | Car |
| Green | Truck |
| Yellow | Van |
| - - - - | - - - - - - - |
如果$ col1和$ COL2是裏面放一個TD標記,這樣做同時獲得$ col1和$ col2顯示在HTML表格的Col1中。但是,$ 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表:
| - - - - - - - - - - - - |
| Column1 |
| - - - - - - - - - - - - |
| Blue Car |
| Green Truck |
| Yellow Van |
| - - - - - - - - - - - - |
是否有可能在HTML表格的列1至回聲$ col1和$ col2的和有每個記錄在它自己的行中的HTML表?
| - - - - - - - - - - - - |
| Column1 |
| - - - - - - - - - - - - |
| Blue |
| Green |
| Yellow |
| Car |
| Truck |
| Van |
| - - - - - - - - - - - - |
你想要的結果就像你的問題中的最後一個數字? –