Jump to content

[SOLVED] Is there code to "ignore" empty fields with "echo"?


spock9458

Recommended Posts

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

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.

 

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'],"&nbsp&nbsp ",$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

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.