简体   繁体   中英

If MySQL row output equals X then do Y

What I'm trying to achieve here is to change the output. The rows minutes, sms or data can output -1 as the result. All I want to do is have it so if the output from any row is -1, then it should output "Unlimited" instead.

This is done with CodeIgniter.

foreach ($getprices->result() as $row)
{
   echo $row->price;
   echo "<br>";
   echo $row->minutes;
   echo "<br>";
   echo $row->sms;   
   echo "<br>";
   echo $row->data;   
   echo "<br><hr>";
}

Simple answer:

echo ($row->price == -1) ? 'Unlimited' : $row->price;

Do this for each output you have.

To use mysql if statement

SELECT price
     , IF(minutes = -1, 'Unlimited', minutes) as minutes
     , IF(sms = -1, 'Unlimited', sms) as sms
     , IF(data = -1,'Unlimited', data) as data
<...>

But I prefer handling it in PHP it self, just like @phpisuber01 suggested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM