2014-11-07 63 views
0

如何將行返回到表格形式並避免字段爲空?返回一行並避免其中的空字段

<?php 
$o = mysqli_query("SELECT * FROM orders ORDER BY po_number DESC LIMIT 1"); 
echo 'The Following Order Information has been submited.'; 
echo '<table width="70%" border="5" align="center">'; 
while($row = mysqli_fetch_array($o)) { 
    echo '<tr>'; 
    foreach($row as $field) { 
    echo '<td>' . htmlspecialchars($field) . '</td>'; 
} 
echo '</tr>'; 
    } 
echo '</table>'; 
?> 
+0

使用'array_filter($行)''之前的foreach()' – MH2K9 2014-11-07 02:53:25

+0

加上'if'條件。 – sectus 2014-11-07 02:53:53

回答

0

foreach()使用前array_filter($row)。它從array()中刪除所有空元素。

echo 'The Following Order Information has been submited.'; 
echo '<table width="70%" border="5" align="center">'; 
while($row = mysqli_fetch_array($o)) { 
    $row = array_filter($row); //This line 
    echo '<tr>'; 
    foreach($row as $field) { 
     echo '<td>' . htmlspecialchars($field) . '</td>'; 
    } 
    echo '</tr>'; 
} 
echo '</table>'; 
+0

它的工作原理!謝謝你 – Joseph 2014-11-07 02:59:48

+0

@Joseph歡迎你! :) – MH2K9 2014-11-07 03:01:02

0
foreach($row as $field) { 
if(isset($field) && !empty($field)) { 
    echo '<td>' . htmlspecialchars($field) . '</td>'; 
}