If a value in a column is NULL all the rows displayed are affected by that column - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Monday, June 7, 2021

If a value in a column is NULL all the rows displayed are affected by that column

Project: Display data from the 4 most recent id's registered on a certain table.

Table "Invoices" (example):

IdInvoice IdCustomer IdAdmin Total
1 8 NULL 122
2 19 NULL 142
3 21 NULL 217
4 NULL 5 43

Objective: For each "IdInvoice", if the "IdCustomer" has a null value it echo the "IdInvoice", "IdAdmin" and "Total", else it echo the "IdInvoice", "IdCustomer" and "Total".

Problem: When it finds a null value, it echo each "IdInvoice" and "Total" correctly but only 1 time the IdAdmin and doesn't display the IdCustomer.

My code:

$sql = SELECT IdInvoice, IdCustomer, IdAdmin, Total FROM Invoices ORDER BY IdInvoice LIMIT 4;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
  while ($row = mysqli_fetch_array($result)) {
      if ($row["IdCustomer"] = " ") {
          echo '<tr>';
          echo '<td>' . $row["IdInvoice"] . '</td>';
          echo '<td>' . $row["IdAdmin"] . '</td>';
          echo '<td>' . $row["Total"] . '</td>';
          echo '</tr>';
      } else {
          echo '<tr>';
          echo '<td>' . $row["IdInvoice"] . '</td>';
          echo '<td>' . $row["IdCustomer"] . '</td>';
          echo '<td>' . $row["Total"] . '</td>';
          echo '</tr>';
        }
    }
  }else {
     echo '<td>Nothing to display</td>';
  }
}

I've searched on many websites, answered questions, videos, etc, but can't find what i need. Maybe im missing something obvious or searching wrong but i'd like some help if possible.

Thank you for the attention!



source https://stackoverflow.com/questions/67862837/if-a-value-in-a-column-is-null-all-the-rows-displayed-are-affected-by-that-colum

No comments:

Post a Comment