2014-11-07 73 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>'; 
}