如果值不在表中,则显示标准消息
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 } ?>
如果数据库表中的条目不存在,我希望能够显示回退语句。 p>
在用户个人资料页面上,我想显示一个电话号码。 如果用户没有输入电话号码,我想显示一条消息,说“没有提供号码”。 p>
目前,如果该值不存在,我就无法显示任何内容,但这并不理想 p>
我将如何修改 我正在使用(下面)实现的当前代码? p>
&lt;?php if(!empty($ profile ['profile_phonenumber'])){?&gt; \ n&lt;?php echo $ profile ['profile_phonenumber']?&gt;
&lt;?php}?&gt;
code> pre>
div>
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>
Use this code with else
clause:
if (!empty($profile['profile_phonenumber'])) {
echo $profile['profile_phonenumber'];
} else {
echo 'No phone provided';
}
Use Ternary operator. Try this code:
<?php
echo (!empty($profile['profile_phonenumber'])) ? $profile['profile_phonenumber'] : "no number has been provided";
?>