How to show only the completed data of a query to the database

Asked

Viewed 48 times

0

In my script, I do the query, but there are some fields that are empty and in foreach empty spaces. How do I only take the data you have filled?

<?php

$consulta = $pdo->query("SELECT * FROM ws_so_wind WHERE ws_so_wind.id_soft = ws_soft_pos_contra.id_soft ");

foreach ($consulta as $pos) { ?>
  <li><?= $pos['pos']; ?></li> 
<?php } ?>

2 answers

1


Just check that the value is not null with the function empty.

foreach ($consulta as $pos) {
  echo !empty($pos['pos']) ? "<li>{$pos['pos']}</li>" : null;
}

If the value is not empty, displays the HTML code of the element li, otherwise, nothing will be displayed.

  • I really like the ternary conditional operator !

1

I entered a condition in your code. You may need to match the condition according to the contents of the table or the fields you want to skip, but this is the model:

<?php
$consulta = $pdo->query("SELECT * FROM ws_so_wind WHERE ws_so_wind.id_soft = ws_soft_pos_contra.id_soft ");

foreach ($consulta as $pos) {
    if(!empty($pos['pos'])) {
        echo "<li>" . $pos['pos'] . "</li>";
    }
} ?>
  • Just be careful that the value checked by the function empty must be $pos['pos'], not only $pos.

  • corrected, vlw!

  • fought for help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.