返回一行并避免其中的空字段

时间:2014-11-07 02:50:05

标签: php

如何将行返回到表格中并避免使用空字段?

<?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>';
?>

2 个答案:

答案 0 :(得分:0)

array_filter($row)之前使用foreach()。它会从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>';

答案 1 :(得分:0)

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