简体   繁体   中英

Hide column from html table if there is no data returned from query

I have an array such:

  $values = array();

Along with a MySQL query:

$query = "SELECT $columns FROM Prices WHERE `key` LIKE '$rows' AND rate LIKE '$rate'";

if($results = $db->query($query)) {
    if($results->num_rows) {
        while($row = $results->fetch_object()) {
            $values[] = $row;
        }
        $results->free();
    }

I've printed out the table using the following code: (I have removed some table columns)

<?php 
if(!count($values)) {
    echo '<p>No results found for your current search. Use the inputs to the left to change the filters.</p>';
 } else {
   ?>
       <table>
           <thead>
               <tr>
                <th>Location</th>
                <th>City</th>
                </tr>
            </thead>
            <tbody>
              <?php
              foreach ($values as $v) {
              ?>
              <tr>
                <td> <?php echo $v->Location; ?> </td>
                <td> <?php echo $v->City; ?> </td>
              </tr>
              <?php
              }
              ?>
            </tbody>
          </table>
        <?php 
        }
        ?>

I can return data from the MySQL query, and print this to the table. However, sometimes not all the columns need be printed as they have no values. I would like to hide these columns.

ie table result may be

| col 1 | col 2 | col 3 | col 4 |
==================================
|       |       |   33  |       |
|       |       |   32  |       |

And would become

| col 3 |
=========
|   33  |
|   32  |

OR

| col 1 | col 2 | col 3 | col 4 |
==================================
|       |       |       |   65  |
|       |       |       |   25  |

would become

| col 4 |
=========
|   65  |
|   25  |

My question is: How would I hide the empty columns?

Note: table data is populated from user via input form.

I am happy to do this via css, php, or JS

Assuming the returned object has column names for keys, first loop over the first row values to get columns names and write the TH tags, then loop over each row and write out the values.

// mocking up values for example
$values = array( (object) array("col_1" => 72, "col_3" => 44, "col_4" => 16), (object) array("col_1" => 51, "col_3" => 87, "col_4" => 34));

// error message if no results
if(!count($values)) {
    echo '<p>No results found for your current search. Use the inputs to the left to change the filters.</p>';

// display results
} else {
?>
 <table border="1">
     <thead>
        <tr>
          <?php foreach ($values[0] as $k => $c) { /* loop first row to get column names */ ?>
            <th> <?php echo $k; ?> </th>
          <?php } ?>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($values as $v) { /* loop rows */ ?>
        <tr>
          <?php foreach ($v as $k => $c) { /* loop columns */ ?>
            <td> <?php echo $c; ?> </td>
          <?php } ?>
        </tr>
        <?php } ?>
      </tbody>
    </table>
<?php } ?>

Output:

| col_1  | col_3 | col_4 |
==========================
| 72     | 44    | 16    |
| 51     | 87    | 34    |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM