Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

1's and 0's as Yes and No 4

Status
Not open for further replies.

d0nny

IS-IT--Management
Dec 18, 2005
278
GB
I'm sure this must be quite simple (so simple that I can't work it out!), but I have values in my DB of 1 and 0 for users who are say, activated.
I've completed an admin page which then lists these users and the value of the Activated column is 1 (or 0). But I want these 1's and 0's converted to Yes and No where necessary.

How is this possible?
I simply have a SELECT statement and then list out the values in my table.
 
Hi

PHP:
$val=array('No','Yes');

$row=fetch_assoc($statement); // assuming all other required operations

echo $val[$row['Activated']];
... or ...
Code:
select case when activated=1 then 'Yes' else 'No' end as activated from users;
Of course specifying what database could help more...

Feherke.
 
Surely, at it's simplest you just need a conditional statement?

In PHP...

Simple If statement
Code:
$active = 'No';

if($row['active']==1){
   $active='Yes';
}

echo($active);


or

Switch statement
Code:
switch($row['active']){
  case 0:
    $active='No';
  break;

  case 1:
    $active='Yes';
  break;
}

echo($active);


or
Ternary Operator
Code:
$active = $row['active'] ? 'Yes' : 'No';
echo($active);


--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
or in mysql itself

Code:
select if(activated = 1, 'yes' ,'no') as activated from tablename where userid=4

Feherke's case syntax will also work for mysql.
 
Not to be left out, you could also do

Code:
if($handle['activated']) { echo 'Yes'; } else { echo 'No'; }

I love it! This is why in our line of work we often say that there is more than one way to skin a cat.

I like to employ the method listed where it is done within the mysql command. This way you can simply reference the returned field and not deal with conditions every time it is needed.


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
I would concur, doing it in MySQL would be better - though I confess to just using a PHP conditional.

I am, perhaps incorrectly, assuming that since the OP originally asked this question (where a simple conditional would have done the job) that they perhaps aren't completely comfortable with SQL.

We also don't know what flavour of SQL is being used.


Code:
if($handle['activated']) { echo 'Yes'; } else { echo 'No'; }

Wouldn't that rely on whether the value tested was tested as a string or an integer? If it's a string then there will always be a value for $handle['activated'] whether it's 1 or 0. Consequently the result would always be true.
I bet jpadie knows ;-)

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
Good point Foamcow ... When I see 1 or 0, I intuitively (I hate this) "assumed" that the field type is INT. Since I understand that using a condition such as above quoted, it should be a legit statement.

1 same as TRUE and 0 same as FALSE.

Please correct me if I am wrong for me to remove various time bombs off my code! :)




--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
it's one of the annoyances of the mysql library that all columns are reported to php as strings. other database libraries do not play in the same way.

however, to some degree of rescue, is the fact that php is so loosely typed. Because of this, type comparison also has quite a bit of implicit type conversion.

so, the string "0" will evaluate to boolean false (providing you are not using strict comparison - ===). whereas ANY OTHER STRING will evaluate to boolean true.

php manual says this here

When converting to boolean, the following values are considered FALSE:

the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
Every other value is considered TRUE (including any resource).
 
So everyone was right! Cool! :)

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
Wow!
I'm bogled with all the responses. Sounds like a really hot topic.
And yes, I'm using mySQL.

The problem I often find with these responses is trying to fit the proposed solutions into my code, as invariably my code is usually more complicated that just this one issue.

I mean, I have a SELECT statement pulling multiple fields from my DB, and then put the results into an array, so taking these solutions in isolation is fine but incorporating these into my existing code is usually harder (for me!).

But thanks everyone for the solutions!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top