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!
In SQL you use 0 and 1 to set a bit field (just as a yes/no field in Access). In Management Studio it displays as a false/true value (at least in recent versions). When accessing the database through ASP.NET it will expose the field as a boolean value.
You can insert a boolean value using the INSERT statement: INSERT INTO testbool (sometext, is_checked) VALUES ('a', TRUE); INSERT INTO testbool (sometext, is_checked) VALUES ('b', FALSE); When you select a boolean value, it is displayed as either 't' or 'f'.
In SQL Server, a Boolean Datatype can be created by means of keeping BIT datatype. Though it is a numeric datatype, it can accept either 0 or 1 or NULL values only. Hence easily we can assign FALSE values to 0 and TRUE values to 1. This will provide the boolean nature for a data type.
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With