Jump to content

display different result depending on mysql number


final60

Recommended Posts

Hi how can I echo one variable that will check the integer from the database and print its associated text.

 

if($status_result ==0){echo "<P>PENDING</p>";};

if($status_result ==1){echo "<p>ACCEPTED</p>";};

if($status_result ==2){echo "<p>DECLINED</p>";};

 

sorry bit of a php noob!

thanks for nany help

 

P.S

 

I have a similar column in database but with only 2 possible options 0 or 1, so I used the ternary operator:

$quote_result2 = $quote_result == 0?'<p>PENDING</p>':'<p>SENT</p>' .

 

 

But not sure best way to did it with more than 2 options.

there are a few ways to acomplish this. first is use an array:

$status = array('PENDING','ACCEPTED','DECLINED');

echo  '<p>'.$status[$status_result].'</p>;

 

another ( and only if you know the output is a limited amount ) is a switch:

$status = array('PENDING','ACCEPTED','DECLINED');
switch($status_result)
{
case 0: $result = 'PENDING';
             break;
case 1:  $result ='ACCEPTED';
             break;
case 2: $result = 'DECLINED';
             break;
}
echo  '<p>'.$result .'</p>;

personally I would use an array to acomplish this, as you may in the future need to do more dynamic actions with your arrays.

thanks for your reply.

That array didn't work in this instance.

 

$status_result2 = array('PENDING','ACCEPTED','DECLINED');

echo $status_result2[$status_result];

 

while($row = mysql_fetch_assoc($query))

{

$status_result = $row['status'];

                }

 

 

 

currently the integer in status on database is 0.

Whay have you got your output above and outside of your query result??????????????????

it should look like this:

 

$status_result2 = array('PENDING','ACCEPTED','DECLINED');
         
         
      while($row = mysql_fetch_assoc($query))
      {
$status_result = $row['status'];
echo $status_result2[$status_result];
         
                 }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.