2016-01-06 88 views
0

的MySQL-PHP顯示數據我想使用HTML頁面內PHP MySQL的表顯示的數據,在HTML表格包裝數據。分別以HTML表格

我有一個MySQL表中的11列和x行,其中我只需要6列的所有行打印爲HTML表格。對此我能夠做到的(成功)

我的方法:

$result = mysqli_query($connect, "SELECT order_id, order_name, order_from, order_to, order_pickup, order_dropoff FROM users"); 
 

 
// Printing results in HTML 
 
echo "\n"; 
 
while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) { 
 
    echo "\t<tr id='$line[order_id]'>\n"; 
 
    foreach ($line as $col_value) { 
 
     echo "\t\t<td>$col_value</td>\n"; 
 
    } 
 
    echo "\t</tr>\n"; 
 
} 
 
echo "\n";

輸出:

enter image description here

我的問題: 其中6列,我想1列命名order_id是行(tr)的id,所以現在我想唯一剩下五成表細胞的td's。我不想將order_id列的值作爲表格單元格。

+5

那麼不要回聲呢?我建議使用'if' – Naruto

+0

divy3993,請檢查我的答案,以查看它是否按預期工作。否則就沒有必要,我會刪除我的答案。 – 2016-01-06 16:30:55

+0

@Naruto檢查我的答案。 – 2016-01-06 16:33:03

回答

2

檢查也列名:

foreach ($line as $col_name => $col_value) { 
    if ($col_name != 'order_id') { 
     echo "\t\t<td>$col_value</td>\n"; 
    } 
} 
+0

'=>'在這裏做什麼? – divy3993

+0

'=>'表示[foreach]的鍵值語法(http://php.net/manual/en/control-structures.foreach.php)。 –

1

你的解決方法很簡單,你只需要檢查,如果數組鍵是order_id,如果是,那就不要呼應它:在$line d

echo "\n"; 
while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) { 

    echo "\t<tr id='$line[order_id]'>\n"; 
    foreach ($line as $key => $col_value) { 
     if ($key != 'order_id') 
     echo "\t\t<td>$col_value</td>\n"; 
    } 
    echo "\t</tr>\n"; 
} 
echo "\n"; 
+0

你讓我錯了,我想order_id作爲各自的'tr'(我能夠實現sucessfull,你在這裏刪除)的id,但不能被打印爲'td'「Table-cell」。 – divy3993

+0

哦好吧我知道一個修復 – Phiter

1

我相信我會發布更好的答案,但我會發布它只是爲了看看這個想法有多簡單。

我的代碼的缺點是,它會檢查每個循環語句if()增加代碼的複雜性。

基本上你設置一個bool變量來保存每個錶行的第一關。該變量設置爲false($pass_one = false)。然後,當我們開始得到列信息,我們設置的布爾變量true並跳過的第一個元素。在下一列迭代中,我們按照我們的要求繼續。在完成列設置後,我們將$pass_one更改回false,並重復執行其他行。

<?php $result = mysqli_query($connect, "SELECT order_id, order_name, order_from, order_to, order_pickup, order_dropoff FROM users"); 

$pass_one = false; //you set it to false 

// Printing results in HTML 
echo "\n"; 
while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) { 
    echo "\t<tr id='$line[order_id]'>\n"; 
    foreach ($line as $col_value) { 
     if (!$pass_one) { 
      $pass_one = true; // you pass the first echo, so you set the bool to true 
      continue; // skip the first pass, (this statement goes to foreach() loop 
     } 
     echo "\t\t<td>$col_value</td>\n"; 
    } 
    echo "\t</tr>\n"; 
    $pass_one = false; // we get a new <tr> so we set the $pass_one for that loop to false 
} 
echo "\n"; 

?> 
2

好的有很多解決方案。通常,在編寫這種類型的代碼時,經常會將數據庫層與模板層分開,並將信息評估到模板層。

爲了您的示例,您可以選擇刪除foreach並單獨定義單獨的列值,在我看來這通常是最好的方法,因爲您已經知道所有列,並且有更多時間控制回顯他們:

$result = mysqli_query($connect, "SELECT order_id, order_name, order_from, order_to, order_pickup, order_dropoff FROM users"); 

// Printing results in HTML 
echo "\n"; 

while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) { 
    echo "\t<tr id='$line[order_id]'>\n"; 

    echo "\t\t<td>$line['title']</td>\n"; 
    echo "\t\t<td>$line['second title']</td>\n"; 

    echo "\t</tr>\n"; 
} 

echo "\n"; 

我相信用戶已經提到的另一種方法是使用if if 語句。你可以在數組的鍵上做到這一點:

$result = mysqli_query($connect, "SELECT order_id, order_name, order_from, order_to, order_pickup, order_dropoff FROM users"); 

// Printing results in HTML 
echo "\n"; 

while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) { 
    echo "\t<tr id='$line[order_id]'>\n"; 

    foreach ($line as $col_name => $col_value) { 
     if ($col_name === 'order_id') { 
      continue; 
     } 
     echo "\t\t<td>$col_value</td>\n"; 
    } 

    echo "\t</tr>\n"; 
} 

echo "\n"; 
+0

是的,因爲我知道我的列名,所以您的解決方案也有幫助。非常感謝你! – divy3993

+0

很好的服務,我已經閱讀了其他一些評論,我認爲你仍然在努力弄清楚PHP中的事情是如何工作的。有這個偉大的網站,可以幫助你稱爲[鏈接] [https://www.codecademy.com]。這應該解釋大部分基礎知識,包括大部分語法,例如=>在foreach –

+0

再次感謝! – divy3993