K20GH
K20GH

Reputation: 6281

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>";
}

Upvotes: 0

Views: 447

Answers (2)

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42889

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.

Upvotes: 4

phpisuber01
phpisuber01

Reputation: 7715

Simple answer:

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

Do this for each output you have.

Upvotes: 6

Related Questions