Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying a blank TD if data in array is null (or empty) 1

Status
Not open for further replies.

KevinFSI

Programmer
Nov 17, 2000
582
US
I'm a longtime CF programmer who is just learning PHP. I'm very excited about too.

Here's my question. Is there a way to have an if statement in the middle of an echo statement? I just want to display a nbsp if the array index is empty. In CF I'd do this:
Code:
<TD>#IIf( myArray[ x ] IS "", DE( "&nbsp;" ), "myArray[ x ] )#</TD>

How do I do this in PHP? Right now, I just have that logic separated out before the TD output where I say:
Code:
 if ( $myArray[ x ] == "" )
   $myVar = "&nbsp;";
else
   $myVar = $myArray[ x ];

This works, but is it the best way?

Kevin
slanek@ssd.fsi.com

&quot;Life is what happens to you while you're busy making other plans.&quot;
- John Lennon
 
You could also try this notation:

Code:
$myVar = ( $myArray[ x ] == "" ) ? "&nbsp;" : $myArray[ x ];

It is a compact if statement that fits on one line.
 
I suppose I could shorten that 2 code example above to:
Code:
$myVar = ( $myArray[ x ] == "" ) ? "&nbsp;" : $myArray[ x ];

But can I not do the above statement inline?

Kevin
slanek@ssd.fsi.com

&quot;Life is what happens to you while you're busy making other plans.&quot;
- John Lennon
 
HAHA, danomac, you beat me to it. So, shouldn't this work?
Code:
echo "<TD>".( $myArray[ x ] == "" ) ? "&nbsp;" : $myArray[ x ]."</TD>";

Or is something like that not an option?

Kevin
slanek@ssd.fsi.com

&quot;Life is what happens to you while you're busy making other plans.&quot;
- John Lennon
 
Or, I guess I could include nbsp when I create the array itself, then I wouldn't have to worry about it when it comes time to output in my TD.

Kevin
slanek@ssd.fsi.com

&quot;Life is what happens to you while you're busy making other plans.&quot;
- John Lennon
 
That will get odd results, but if you enclosed it in brackets it should work:

Code:
echo "<TD>". ( ( $myArray[ x ] == "" ) ? "&nbsp;" : $myArray[ x ] ) ."</TD>";
 
danomac said:
That will get odd results, but if you enclosed it in brackets it should work

Perfect, thank you.

Kevin
slanek@ssd.fsi.com

&quot;Life is what happens to you while you're busy making other plans.&quot;
- John Lennon
 
Just in case others are wondering, this is the reason it needs brackets:

The interpreter will go along and echo the opening tag for the cell like normal. If the array being tested is not null it will echo the value and the closing tag. However, if the array is empty, all it will echo is the nonbreaking space. It won't include the closing table cell tag.

I've broken it down and colourized it in case you don't understand:

(Original line interpretation)
Code:
echo "<TD>".[COLOR=blue]( $myArray[ x ] == "" ) [/color] ? [COLOR=green]"&nbsp;"[/color] : [COLOR=red]$myArray[ x ]."</TD>"[/color];
By setting up the brackets, you set the boundaries for the inline if statement:
(Fixed line interpretation)
Code:
echo "<TD>". [COLOR=gray yellow]( [COLOR=blue]( $myArray[ x ] == "" )[/color] ? [COLOR=green]"&nbsp;"[/color] : [COLOR=red]$myArray[ x ][/color] )[/color] ."</TD>";

Hopefully that explains what happens a little better.
 
You should note that variables in PHP are preceded by the dollar sign. If the 'x' in the example code refers to an incrementing variable it should be $x in all cases.
Otherwise PHP will look for a defined constant by the name of 'x' and if not found revert to the string 'x' itself, which would point to the key 'x' in an associative array.
 
DRJ478 said:
You should note that variables in PHP are preceded by the dollar sign. If the 'x' in the example code refers to an incrementing variable it should be $x in all cases.
Otherwise PHP will look for a defined constant by the name of 'x' and if not found revert to the string 'x' itself, which would point to the key 'x' in an associative array.

My apologies, you're right. I just whipped up an example and neglected the dollar sign. The example didn't reflect my actual code. Thanks for keeping me on my toes though.

Kevin
slanek@ssd.fsi.com

&quot;Life is what happens to you while you're busy making other plans.&quot;
- John Lennon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top