如何不显示空行?

时间:2016-11-04 15:50:32

标签: php html mysqli

我的问题是:

如果你看一下它会显示的位置"

<td>1ST<?php echo $first ?></td>

&#34;

如果行与变量&#39; $ first&#39;相关联,我如何确保或所有其他如果他们是空的没有任何表现。还有那个&#39; 1st&#39;没有显示?

我尝试了各种各样的事情!

&#13;
&#13;
<h2><a href="#"><?php echo $show_title ?></a></h2>
<h4>Show Date: <span class="glyphicon glyphicon-time"> </span><?php echo $show_date ?></h4>
<hr>
</a>
<hr>
<?php

// SO UPDATE THE QUERY TO ONLY PULL THAT SHOW'S DOGS
$query = "SELECT * FROM result
WHERE first IS NOT NULL";

$result = mysqli_query($connection, $query) or trigger_error
("Query Failed! SQL: $query - Error: ". mysqli_error
($connection), E_USER_ERROR);

if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$dog_name = $row['dog_name'];
$placement = $row['placement'];
$class_name = $row['class_name'];
$entries = $row['entries'];
$absentee = $row['absentee'];
$entries = $row['entries'];
$first	 = $row['first'];
$second = $row['second'];
$third = $row['third'];
$RES = $row['RES'];
$VHC = $row['VHC'];
$DCC = $row['DCC'];
$RDCC = $row['RDCC'];
$BCC = $row['BCC'];
$RBCC = $row['RBCC'];
$BOB = $row['BOB'];
$BP = $row['BP'];
$BJ = $row['BJ'];

?>

<table class="table" border="0"></div>
<tr>
  <td><strong><?php echo $class_name ?></strong> - <h6>Entries: <?php echo $entries ?> Absentees: <?php echo $absentee ?></h6></td>
  <td></td>
</tr>
<tr>
  <td>DCC</td>
  <td><?php echo $DCC ?></td>
</tr>
<tr>
  <td>RDCC</td>
  <td><?php echo $RDCC ?></td>
</tr>
<tr>
  <td>BCC</td>
  <td><?php echo $BCC ?></td>
</tr>
<tr>
  <td>RBCC</td>
  <td><?php echo $RBCC ?></td>
</tr>
<tr>
  <td>BOB</td>
  <td><?php echo $BOB ?></td>
</tr>
<tr>
  <td>BP</td>
  <td><?php echo $BP ?></td>
</tr>
<tr>
  <td>BJ</td>
  <td><?php echo $BJ ?></td>
</tr>

<tr>
  <td>1ST</td>
  <td><?php echo $first ?></td>
</tr>
<tr>
  <td>2ND</td>
  <td><?php echo $second ?></td>
</tr>
<tr>
  <td>3RD</td>
  <td><?php echo $third ?></td>
</tr>
<tr>
  <td>RES</td>
  <td><?php echo $RES ?></td>
</tr>
<tr>
  <td>VHC</td>
  <td><?php echo $VHC ?></td>
</tr>
</table>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

您做得很好,只需对您收到的每一行应用过滤功能:

// SO UPDATE THE QUERY TO ONLY PULL THAT SHOW'S DOGS
$query = "SELECT * FROM result
WHERE first IS NOT NULL";

$result = mysqli_query($connection, $query);

if (!$result) {

    trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($connection), E_USER_ERROR);

} else {

    // Fetch results into array
    $data = mysqli_fetch_all($result, MYSQLI_ASSOC);

    // If results array is not empty
    if ($data) {


        echo '<table class="table" border="0"></div>
        <tr>
          <td>
            <strong><?php echo $class_name ?></strong> - <h6>Entries: <?php echo $entries ?> Absentees: <?php echo $absentee ?></h6>
          </td>
          <td></td>
        </tr>';

        // Now let's walk through every record
        array_walk($data, function($dogRecord) {

            // Here we apply array_filter to each dog record, so that empty values (i.e. those evaluating to false) are filtered out
            $dogRecord = array_filter($dogRecord);


            // Now loop throw $dogRecord to build table

            $collation = [
                'DCC' => 'DCC',
                'RDCC' => 'RDCC',
                'BCC' => 'BCC',
                'RBCC' => 'RBCC',
                'BOB' => 'BOB',
                'BP' => 'BOB',
                'BJ' => 'BOB',
                '1ST' => 'first',
                '2ND' => 'second',
                '3RD' => 'third',
                'RES' => 'RES',
                'VHC' => 'RES'
            ];

            foreach ($dogRecord as $property => $value) {

                echo '<tr>
                    <td>'.$collation[$property].'</td>
                    <td>'.$value.'</td>
                </tr>';

            }

        });


        echo '</table>';
    }

}

请注意,我使用foreach函数代替简单的array_walk循环。这是因为,因为您为每条记录提取变量,所以每次都需要未声明(即未占用)的变量。

答案 1 :(得分:-1)

这个怎么样:

if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
        // Show the table-row
    }
}

答案 2 :(得分:-2)

以下代码将遍历结果中的每一行(while循环),每个key =&gt;每行中的值(foreach循环);然后它将检查一个值是否为空或键是否不等于“第一个&#39; (if语句)并在表元素中回显结果。我不确定我是否完全理解你的问题,但我希望这会有所帮助。

<table>
<?php
if($result){
while($row = mysqli_fetch_assoc($result))
    foreach($row as $key => $value){
        if($value != null && $key != 'first'){
            echo '<tr>';
            echo '<td>' . $key . '</td>';
            echo '<td>' . $value . '</td>';
            echo '</tr>';
        }
    }
}
?>
</table>