shmideo Posted November 27, 2014 Share Posted November 27, 2014 Hi, I have results from a MySQL table column which does an echo into a table cell, but some of the results from the table column is be empty, so is there a way to fill in the blanks with text like "No data"? Quote Link to comment https://forums.phpfreaks.com/topic/292746-how-to-add-text-to-a-null-result-in-a-table/ Share on other sites More sharing options...
Ch0cu3r Posted November 27, 2014 Share Posted November 27, 2014 Yes. Use empty to see if the column is empty then echo "No data" if it is. Quote Link to comment https://forums.phpfreaks.com/topic/292746-how-to-add-text-to-a-null-result-in-a-table/#findComment-1497823 Share on other sites More sharing options...
Barand Posted November 27, 2014 Share Posted November 27, 2014 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 | +--------+------------+-----------+ Quote Link to comment https://forums.phpfreaks.com/topic/292746-how-to-add-text-to-a-null-result-in-a-table/#findComment-1497833 Share on other sites More sharing options...
shmideo Posted November 27, 2014 Author Share Posted November 27, 2014 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/292746-how-to-add-text-to-a-null-result-in-a-table/#findComment-1497862 Share on other sites More sharing options...
mikosiko Posted November 27, 2014 Share Posted November 27, 2014 Do you have a field named 'no Data'? That is what $row['no Data'] means... Maybe you want just echo the literal'no Data'? Quote Link to comment https://forums.phpfreaks.com/topic/292746-how-to-add-text-to-a-null-result-in-a-table/#findComment-1497864 Share on other sites More sharing options...
Solution Ch0cu3r Posted November 27, 2014 Solution Share Posted November 27, 2014 $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']; Quote Link to comment https://forums.phpfreaks.com/topic/292746-how-to-add-text-to-a-null-result-in-a-table/#findComment-1497865 Share on other sites More sharing options...
shmideo Posted November 27, 2014 Author Share Posted November 27, 2014 Yes Ch0cu3r, that did the trick! Thanks for all the help, really appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/292746-how-to-add-text-to-a-null-result-in-a-table/#findComment-1497868 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.