Mantas Stanionis
Mantas Stanionis

Reputation: 121

Find null values returned by SQL query

I am calling an SQL statement which selects everything from a view. There might be some null values returned, I would like to find them and highlight them in the HTML document. This is my code so far. How can I find the empty columns (which can be seen in the picture) so I could highlight them with CSS?

Thanks.


<?php 
require_once '../includes/header.php'; 

$sql = $conn->prepare("SELECT * FROM vw_allpropertiesandagents ORDER BY Price");
$sql->execute();
?>

<section class="main-sec">
<h1>All Agents and All properties</h1>
<p>The properties even the ones that have no agents assigned to them are displayed</p>
<table class ='dba-table'>
    <tr>
        <th>Property Id</th>
        <th>Full Address</th>
        <th>Price</th>
        <th>Property Agent Id</th>
        <th>Agent Id</th>
        <th>For</th>
        <th>Property Type</th>
    </tr>
<?php while ($row = $sql->fetch()){ ?>
    <tr>
    <?php 

    foreach ($row as $value){ ?>
        <td><?php echo $value?></td>
    <?php } ?>
    </tr>


    <?php } ?>
</table>
</section>


<?php 
require_once '../includes/footer.php'; 

?>

This is the HTML output]1

Upvotes: 0

Views: 47

Answers (1)

user11917850
user11917850

Reputation:

If I understand correctly you are looking for something like this:

<?php foreach ($row as $value): ?>
   <?php if($value === null): ?>
      <td style="background-color: red;">Empty</td>
   <?php else: ?>
      <td><?= $value ?></td>
   <?php endif; ?>
<?php endforeach; ?>

Please be aware that this code is vulnerable to XSS because you are not escaping the data when echo'ing. I would recommend to only use this code locally for learning purposes.

There are some excellent articles on the internet, which you can read to learn how to prevent XSS injection.

Upvotes: 1

Related Questions