0

I need the proper php syntax to handle a situation with my website's breadcrum links for product pages. All of my items are derived from a MySQL database.

The inventory items are organized either by itemCategory or itemAuthor. Not all items have an itemCategory associated with them so in the database they are NULL. All items however, have an itemAuthor.

What I basically want to say is:

If the item has a value for itemCategory, echo the itemCategory. If itemCategory is NULL, echo itemAuthor instead.

Thanks for any help.

1
  • echo !is_null($row['itemCategory']) ? $row['itemCategory'] : $row['itemAuthor'];? Commented Dec 14, 2011 at 19:47

2 Answers 2

3
if(empty($itemCategory)) {
  echo $itemAuthor;
} else {
  echo $itemCategory;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm a little baffled that you're managing breadcrumbs and MySQL access but not if/else statements...
0

The mysql solution:

SELECT ...
    IFNULL(`itemCategory`, `itemAuthor`) AS `itemCategory`
FROM ...

In PHP You just echo itemCategory. No additional conditions. If it was NULL id database, it will be itemAuthor in PHP.

The php solution:

if ($r['itemCategory']):
    echo $r['itemCategory'];
else:
    echo $r['itemAuthor'];
endif;

The NULL is returned as empty string. It is converted to php false in if expressions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.