spock9458 Posted May 27, 2009 Share Posted May 27, 2009 I've searched all over for the answer to this, and I am pretty sure I just don't know how to word the question briefly. I am building an online directory for a fairly large organization, and I have worked out the code for the mysql queries and the php to format the results the way I want them to look. But with one slight cosmetic flaw. Besides the standard member data (name, address, etc.) there are some fields in my table for website url, email address, which some but not all of the members contain entries in those fields. In other words, for some members these fields are simply blank. When I format the output to display nicely in my table with the "echo" command, it works fine for the members who have data in all fields... but for the ones with blank fields it creates unsightly "extra" blank lines. I want to know if there is a way to display (echo) my results, but with some sort of command or function that will "ignore" the blank field results and not create blank lines. I can't believe it's so difficult to ask this question... I know I lack the proper terminology being the PHP "hack" that I am. Hope someone can help me. Thanks, Rob Link to comment https://forums.phpfreaks.com/topic/159815-solved-is-there-code-to-ignore-empty-fields-with-echo/ Share on other sites More sharing options...
anupamsaha Posted May 27, 2009 Share Posted May 27, 2009 Try this: If you have "empty" filled in the field: echo (empty($yourDataField) ? '' : $yourDataField); If you have "NULL" filled in the field: echo (is_null($yourDataField) ? '' : $yourDataField); Cheers! Hope this will help. Link to comment https://forums.phpfreaks.com/topic/159815-solved-is-there-code-to-ignore-empty-fields-with-echo/#findComment-842927 Share on other sites More sharing options...
savagenoob Posted May 27, 2009 Share Posted May 27, 2009 Not sure what your wanting, you are building tables with the database data and dont want the blank data to show "lines"? Try building data via CSS instead of tables, might help you. Just my 2 cents. Link to comment https://forums.phpfreaks.com/topic/159815-solved-is-there-code-to-ignore-empty-fields-with-echo/#findComment-842928 Share on other sites More sharing options...
spock9458 Posted May 27, 2009 Author Share Posted May 27, 2009 OK, I've tested some things and still cannot accomplish my goal. I am using a "while" loop to echo my results in a table row. My results are going like this: // Here is the "while" loop while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table row echo "<tr><td><b>"; echo $row['comp_name'],"<br>"; echo $row['comp_uw']},"</b><br>"; echo $row['comp_street'],"<br>"; echo $row['comp_csz'],"<br>"; echo $row['comp_ph'],"   ",$row['comp_fx'],"<br>"; echo $row['comp_email'],"<br>"; echo $row['comp_web']; echo "<br>"; What I would like to do is check "empty($row['comp_uw']" (for example) and if it is empty I don't want it to add an empty line to my output. I tried this: if (empty($row['comp_uw'] { // do nothing } else { echo $row['comp_uw']},"</b><br>";} This gives me an error about an unexpected "}" on the line after "//do nothing". I either cannot do what I want this way, or my syntax is all messed up. Please help if you can. Rob Link to comment https://forums.phpfreaks.com/topic/159815-solved-is-there-code-to-ignore-empty-fields-with-echo/#findComment-843343 Share on other sites More sharing options...
spock9458 Posted May 27, 2009 Author Share Posted May 27, 2009 I saw the error in my code... it should be: if (empty($row['comp_uw'])) I forgot the closing parentheses "))" When I added them, it works! Rob Link to comment https://forums.phpfreaks.com/topic/159815-solved-is-there-code-to-ignore-empty-fields-with-echo/#findComment-843352 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.