Ben Ben - 1 year ago 30
PHP Question

If a value isn't in table, display a standard message

I want to be able to display a fallback statement if an entry in my database table doesn't exist.

On a user profile page I would like to display a phone number. If a user hasn't entered a phone number I would like to display a message to say 'no number has been provided.'

At present, I have been able to display nothing if the value doesn't exist, but this isn't ideal

How would I amend the current code I am using (below) to achieve this?

<?php if(!empty($profile['profile_phonenumber'])){ ?>
<?php echo $profile['profile_phonenumber'] ?>
<?php } ?>


You can do it several way: Through php:

echo  (!empty($profile['profile_phonenumber'])) ?
     $profile['profile_phonenumber'] : 'No phone provided';

if you are using mysql: then in select query:

select if(length(profile_phonenumber)>0,profile_phonenumber,'profile_phonenumber') as profile_phonenumber,<other column name>  from <tablename> where <your_condition>