dave
dave

Reputation: 1009

Show text if information has been updated else hide

I'm wondering if someone can help me. I'm sure this will be pretty simple.

I have a form that users can update their account info in. On the profile it prints this info out. But if its empty is their anyway of hiding the text beside it.

FOR EXAMPLE I would like to hide 'Works at' if $data['work'] is empty.

<?php echo "Works at ".$data['work']."" ?> 

I was going to try a case and break with status 1 in the database. But with their being so much info I don't think its possible.

Upvotes: 0

Views: 431

Answers (3)

theGreener
theGreener

Reputation: 21

You can use the ternary operator, checking that an empty space hasn't been provided by trimming first

echo trim($data['work']) != '' ? 'Works at ' . $data['work'] : '';

Its also a good practice to escape output using htmlentities

echo trim($data['work']) != '' ? 'Works at ' . htmlentities($data['work'], ENT_QUOTES, 'UTF-8') : '';

Upvotes: 0

Besnik
Besnik

Reputation: 6529

<?php if ($data['work'] != ""): ?>
  Works at <?php echo $data['work']; ?>
<?php endif; ?>

Or

<?php
if ($data['work'] != "") {
  echo "Works at ".$data['work'];
}
?>

Or

<?php
echo ($data['work'] != "") ? "Works at ".$data['work'] : null;
?>

You can use trim-function to be sure that a string with only whitespaces will be matched by this condition too: trim($data['work'])

Upvotes: 0

32bitfloat
32bitfloat

Reputation: 771

<?php
if(!empty($data['work']))
  echo "Works at ".$data['work'];
?>

edit: to be really sure that the user did not type in some whitespaces, you can extend it bei trimming the value:

<?php
$trimmed = trim($data['work']);
if(!empty($data['work']))
  echo "Works at ".$data['work'];
?>

Upvotes: 2

Related Questions