homepage Welcome to WebmasterWorld Guest from 54.145.183.190
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
if - inline statement
esso82



 
Msg#: 4416660 posted 7:05 pm on Feb 11, 2012 (gmt 0)

hi guys ... I have this line of code and it just doesn't work for me
$boxcont .= "<td class='list1'>".($routes2show['routes_lube'] == "0") ? 'none' : 'lubed'."</td>";

$routes2show['routes_lube'] is read from database, it is integer and it's equal to 0 (zero) ... the problem is it doesn't give any output..where am I wrong?

 

esso82



 
Msg#: 4416660 posted 7:23 pm on Feb 11, 2012 (gmt 0)

an error obviously occurred between keyboard and chair :)

$boxcont .= "<td class='list1'>".($routes2show['routes_lube'] == "0" ? 'none' : 'lubed')."</td>";

problem solved ;)

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4416660 posted 5:40 pm on Feb 13, 2012 (gmt 0)

That shouldn't have made any difference. This is a perfectly valid ternary.

$boxcont .= "<td class='list1'>".($routes2show['routes_lube'] == "0") ? 'none' : 'lubed'."</td>";

it's equivalent to this.


if ($routes2show['routes_lube'] == "0") {
$boxcont .= 'none';
}
else {
$boxcont .= 'lubed';
}

I only see two things:

1. The variable may not be SET. This can be tested with isset.
2. You are evaluating an integer as a string by quoting it.

Both together try this:

$boxcont .= "<td class='list1'>".(isset($routes2show['routes_lube']) and ($routes2show['routes_lube'] == 0)) ? 'none' : 'lubed'."</td>";

A better choice would be to check for the row values **before** you get to this part, for example,

$boxcont .= "<td class='list1'>";
if (isset($routes2show['routes_lube']) {
$boxcont .= ($routes2show['routes_lube'] == 0) ? 'none' : 'lubed';
}
else { $boxcont .= 'N/A'; }
$boxcont .= "</td>";

If that "N/A" is not supposed to be there, it will give you a clue something is wrong with the data.

[edited by: rocknbil at 5:44 pm (utc) on Feb 13, 2012]

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4416660 posted 5:43 pm on Feb 13, 2012 (gmt 0)

<oops double-post>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved