Reputation: 13216
I have a field on a read only SQL table called attended can only hold two values either 0 or 1.
This is how I am printing the field at the moment:
echo "<td>" . $row['attended'] . "</td>";
It only returns 0 or 1 - the value in the attended field. How can I have it return no for 0 (i.e not attended) or yes for 1 (i.e. attended).
Many thanks in advance!
Upvotes: 5
Views: 11537
Reputation: 3224
Something like:
echo('<td>'.(($row['attended']==1) ? 'yes' : 'no').'</td>');
Upvotes: 0
Reputation: 4994
$arr = array(1 => 'Yes', 0 => 'No);
echo "<td>" . $arr[$row['attended']] . "</td>";
Upvotes: 2
Reputation: 74106
Use the ternary operator.
echo "<td>" . ( $row['attended'] == 1 ? 'yes' : 'no' ). "</td>";
Upvotes: 0
Reputation: 265956
The ternary operator is a good fit:
echo ($row['attended']?'yes':'no');
If you want to get yes/no directly from your sql query, use an IF
statement:
SELECT IF attended THEN 'yes' ELSE 'no' AS attended
FROM …
Upvotes: 1
Reputation: 839254
You can use the ternary operator (also known as the conditional operator in some languages) ?:
:
echo '<td>' . ($row['attended'] ? 'yes' : 'no') . '</td>';
This operator is mentioned in the manual page Comparison Operators under the heading "Ternary Operator".
Upvotes: 10