Vegard Berg Vegard Berg - 2 months ago 9
MySQL Question

Convert Enum '1', '0' to output text PHP

I have column in my database that check if the module is open or closed. If the column is 0 its closed and 1 = open. How can I change the 0 to closed and 1 to open in my output?

$getModuler = mysql_query("SELECT * FROM `cms_moduler` WHERE code = '".$prosjekt['code']."'");
while($modulinfo = mysql_fetch_array($getModuler))
{
Echo 'Status: '".$modulinfo['locked'].'"'; // <- Returns 0 or 1

Answer

Use a ternary operator to make the simple on/off decision and load a var with the text you require.

while($modulinfo = mysql_fetch_array($getModuler)) {
    $state = $modulinfo['locked'] == 0 ? 'Closed' : 'Open';
    echo 'Status: ' . $state;
}