Jump to content

How to add text to a null result in a table


shmideo
Go to solution Solved by Ch0cu3r,

Recommended Posts

Or you can do it in the query

mysql> SELECT * FROM user;
+--------+------------+-----------+
| iduser | first_name | last_name |
+--------+------------+-----------+
|      1 | Stella     | NULL      |
|      2 | Amanda     | Brown     |
|      3 | Kevin      | Green     |
|      4 | John       | Wilson    |
|      5 | Helen      | NULL      |
|      6 | Pete       | Doone     |
+--------+------------+-----------+

mysql> SELECT iduser
    -> , first_name
    -> , IFNULL(last_name, '(No data)') as lastname
    -> FROM user;
+--------+------------+-----------+
| iduser | first_name | lastname  |
+--------+------------+-----------+
|      1 | Stella     | (No data) |
|      2 | Amanda     | Brown     |
|      3 | Kevin      | Green     |
|      4 | John       | Wilson    |
|      5 | Helen      | (No data) |
|      6 | Pete       | Doone     |
+--------+------------+-----------+
Link to comment
Share on other sites

Thanks guys but still not filling in blank cells. $dstchannel seems to be the problem but don't know how to validate. 'dstchannel' echo's the non-empty rows ok as does the 'calldate' and 'clid'

echo "<tr><td width='20%'>";
echo $row['calldate'];
echo "</td><td width='30%'>";
echo $row['clid'];
echo "</td><td width='30%'>";
if (empty($dstchannel)) {
echo $row["no Data"];
} else {
echo $row['dstchannel'];
}
echo "</td><td width='20%' style='text-align:right'>";
echo $row['duration'];
echo "</td></tr>";
Link to comment
Share on other sites

  • Solution

$row['dstchannel'] is the variable you want to check

if (empty($row['dstchannel'])) {
echo "No Data";
} else {
echo $row['dstchannel'];
}


// or could be written as the following using a Ternary Operator
echo empty($row['dstchannel']) ? 'No Data' :  $row['dstchannel'];
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.