Search code examples
phphtmlxamppsqlsrv

if else for PHP ternary operator using datatable


May I ask for help regarding my DataTable? I have a concern regarding ternary operators. It doesn't recognize the parameters I set in for the table.

This is my table.

echo "
<tr>
<td>".$row['USERID']."</td>
<td>".$row['FINGERSCANNO']."</td>
<td>".$row['Date']."</td>
<td>".$row['Time']."</td>
<td>".(($row['CheckType']=='I') ? 'TIME-IN' : ($row['CheckType']=='O') ? 'TIME-OUT' : ($row['CheckType']=='i') ? 'OVERTIME-IN': ($row['CheckType']=='o') ? 'OVERTIME-OUT' : 'N/A')."</td> 
<td>".(($row['VERIFY']==1)?'OK':"NEEDS VERIFICATION")."</td>
</tr>
";
}
?>
</tbody>
</table>
</div>

This line

<td>".(($row['CheckType']=='I') ? 'TIME-IN' : ($row['CheckType']=='O') ? 'TIME-OUT' : ($row['CheckType']=='i') ? 'OVERTIME-IN': ($row['CheckType']=='o') ? 'OVERTIME-OUT' : 'N/A')."</td>

only works when I do it like this

<td>".(($row['CheckType']=='I') ? 'TIME-IN' : 'TIME-OUT')."</td>

Is there a fix to this? the value of CHECKINOUT.CHECKTYPE AS CheckType are I, O, i, o.


Solution

  • You have to wrap certain parts otherwise the reader does not know what depends on what. Try this:

    <td>".(($row['CheckType']=='I') ? 'TIME-IN' : (($row['CheckType']=='O') ? 'TIME-OUT' : (($row['CheckType']=='i') ? 'OVERTIME-IN': (($row['CheckType']=='o') ? 'OVERTIME-OUT' : 'N/A'))))."</td>
    

    PS: actualy it´s not that good to use nested ternary operators, because it´s bad to read.