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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.