【问题标题】:How to Not display empty rows?如何不显示空行?
【发布时间】:2017-03-18 12:49:39
【问题描述】:

我的问题是:

如果你看看它会在哪里显示“

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

"

我如何确保如果该行与变量 '$first' 相关联,或者如果它们为空,则所有其他行都没有显示。 '1st' 也没有显示?

尝试了各种让我很难过的事情!

<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>

【问题讨论】:

  • (伪'ish):if(row is empty){...} 或三元运算符。
  • 它们为空时不会显示任何内容。还是要删除整个 html 表格行?然后,您需要在每个条件周围添加条件。为什么要将数组复制到不同的变量?您也可以使用下面的数组。
  • @jeroen 也许他们会回应给出的答案; 得跑
  • 你不能再清楚一点吗?如果所有值都为空或只有一个为空,您不想显示该行?

标签: php html mysqli


【解决方案1】:

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

// 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>';
    }

}

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

【讨论】:

    【解决方案2】:

    这个呢:

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

    【讨论】:

    • 我不认为这就是问题所在;再读一遍。
    • 基本上,如果该行为空,则不会显示第 1 行或第 2 行等
    【解决方案3】:

    以下代码将遍历结果中的每一行(while 循环)和每一行中的每个 key => 值(foreach 循环);然后它将检查一个值是否不为空或键不等于'first'(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>
    

    【讨论】:

    猜你喜欢
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    相关资源
    最近更新 更多