Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading