0

Please help with this if you can

My example code below is part of a table that echos several fields, for this field I need to echo the text of the field which the below example code now does, but i need to make it state N/A if the field is Null also which i guess would be with a if/else command but got no idea how to fit it in with the current code

echo $row['model'];
	echo "</td><td>";

Thanks for your help and time

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by _Rade
0
echo is_null($row['model']) ? "N/A" : $row['model'];

This is an inline if/else statement. If the "is_null" condition is satisfied, the output will be "N/A", else the value of your array item.

If the output is a number you could also use $row > 0 instead of is_null($row).

Let us know if this helps :)

0

thanks for your help

it does change to null but all are changed to null

0

Really sorry it does work, my fault. Thanks for your very quick help

0

Otherwise can echo with if else condition

if ($row['model']=="")
{
echo "N/A";
}
else
{
echo $row['model'];
}
0

Yes I did think of that one but the poster stipulated that the value returned would be NULL (NULL) rather than an empty string (""). For a more complete statement you might use something like:

if (!isset($row['model']) || $row['model'] == "" || $row['model'] === FALSE || $row['model'] === NULL || $row['model'] === 0)
{
    echo "N\A";
}
else
{
    echo $row['model'];
}

Depends upon the data types you're working with, but PHP tends to be pretty smart (or dumb, depends who you ask!).

The code in my first post is cleaner but less comprehensive, if it works (after thorough testing!) use it.

Remember to rep posts ;p

Edited by _Rade: pressing tab the enter submitted the post, rather than inserting a tab-space... habit ;o

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.