2016-05-07 79 views
0

是否可以將來自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      | 
| - - - - - - - - - - - - | 
+0

你想要的結果就像你的問題中的最後一個數字? –

回答

1

不需要有兩個循環。它可以在單個循環中完成。用不同的變量分隔col1和col2的值。並在最後打印出來。

<?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>"; 
?> 
+0

這完美的作品。非常感謝Aju的解決方案! – JeremyCanfield

+0

很高興聽到...!謝謝:) –

+0

@FrayneKonok:抱歉的男人..沒有讓你.. –

1

您的HTML有效,但不正確。這可能是結果表的結構。

創建一個新陣列,其中存儲col2值,用於在所有col1值之後顯示。在完成顯示的第二個col2值後,再次完成col1值循環。

$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>"; 
+0

這是一個迷人的解決方案。我沒有考慮在「while」循環之後添加「for」循環。我將試驗這個。這實際上也會對我正在處理的場景有用,因爲我實際上簡化了這個問題以使問題更易於消化:) – JeremyCanfield

+0

是的,@AjuJohn對這個問題有很好的答案,你必須和他一起去。 –

相關問題