gary falkland gary falkland - 1 month ago 4
HTML Question

How to Not display empty rows?

My question is:

If you look at where it would display "

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


"

How do I ensure that if the row associate to variable '$first' or all the others if they are empty nothing shows. Also that the '1st' doesn't show?

Have tried various things I am stumped on this!



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




Answer

You're doing well, just apply a filtering function to each row you recieve:

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

}

Note that instead of simple foreach loop I'm using array_walk function. This is because since you extract variables for each record, you want undeclared (i.e. unoccupied) varables every time.