A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearbooleans with PHP [duplicate] - page 1
User InfoPosts
booleans with PHP [duplicate]#1
Dup of http://www.momige.com/330709/some-basic-php-questions#330829

Hello,

I have a heap of tinyint fields in a mysql database I need to show with PHP. At the moment I am doing something like this:

if ($row['PAYPAL_ACCEPT'] == "1"){

$paypal = "Yes";

else

$paypal = "No";

}


For each field, which is tedious and seems like there should be a better way, than using an if clause for each field. If there is, what is it?

posted date: 2008-12-10 06:13:00


Re: booleans with PHP [duplicate]#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-10 06:13:01


Re: booleans with PHP [duplicate]#3
Try if you want:$paypal = $row['PAYPAL_ACCEPT'] ? 'YES' : 'NO';

posted date: 2008-12-10 06:15:00


Re: booleans with PHP [duplicate]#4
Something like$paypal = ($row['PAYPAL_ACCEPT'] ? "Yes" : "No");maybe?

posted date: 2008-12-10 06:16:00


Re: booleans with PHP [duplicate]#5
You could write it shorter like:$paypal = ($row['PAYPAL_ACCEPT']?'Yes':'No');

posted date: 2008-12-10 06:17:00


Re: booleans with PHP [duplicate]#6
or you can use not tinyint but enum with values 'Yes' and 'No', then simple output field$paypal = $row['PAYPAL_ACCEPT'];

posted date: 2008-12-10 06:19:00


Re: booleans with PHP [duplicate]#7
This is called a Ternary Operator by the way: us3.php.net/language.operators.comparison.

posted date: 2008-12-10 06:20:00


Re: booleans with PHP [duplicate]#8
Same question as the third one here : www.momige.com/330709/…. Closed.

posted date: 2008-12-10 06:28:00


Re: booleans with PHP [duplicate]#9
Building on what's already been suggested:// $columns = string array of column names// $columns = array('PAYPAL_ACCEPT' ... );foreach($columns as $column) { $$column = $row[$column] ? 'YES' : 'NO';}then you can access variables using the column names as variable names:print $PAYPAL_ACCEPT;

posted date: 2008-12-10 06:32:00


Re: booleans with PHP [duplicate]#10
Would this matter that it included columns that were not booleans?

posted date: 2008-12-10 06:52:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.