Including font awesome class in php table row based on a condition

I am working on this below php page and I am trying to include font awesome class when there is a certain condition met. But, getting the below error.

Parse error: syntax error, unexpected ‘if’ (T_IF), expecting ‘;’ or ‘,’

here is the code

foreach($result as $row) {  
    echo '<tr>
            <td>' if(.$row["FLAG"]=="1") { '<i class="fa-sharp fa-solid fa-flag-pennant"></i>';}.'</td>
            <td>'.$row["CREATEDDATE"].'</td> 
            <td>'.$row["ID"].'</td> 
            <td>'.$row["CUSTOMER_NAME"].'</td> 
            <td>'.$row["VEHICLENO"].'</td> 
            <td>'.$row["SERIAL"].'</td> 
        </tr>';  
}  

>Solution :

You’re currently working in an echo statement, and echo if(...) is not valid.

This is what the Ternary operator ($condition ? 'Yes' : 'No') is for:

foreach($result as $row) {  
  echo '<tr>
    <td>' . ($row['FLAG'] == 1 ? '<i class="fa-sharp fa-solid fa-flag-pennant"></i>' : '' ) . '</td>
    <td>' . $row['CREATEDDATE'] . '</td> 
    <td>' . $row['ID'] . '</td> 
    <td>' . $row['CUSTOMER_NAME'] . '</td> 
    <td>' . $row['VEHICLENO'] . '</td> 
    <td>' . $row['SERIAL'] . '</td> 
  </tr>';  
}  

Docs here:

https://www.php.net/manual/en/language.operators.comparison.php

Leave a Reply