Skip to content
Advertisement

Single IF statement vs multiple IFs statements in array obtained from SQL table [closed]

I have a table displaying results from a SQL query.

However, for some exceptional rows (where SOURCE=”ABC”), I would like the data to be retrieved a different table.

Is it better to have an IF that checks the SOURCE and then print the row with each data accordingly? Or is it better to have multiple IFs in each box and keep on checking if SOURCE=”ABC” and then display data from either one or the other table?

This is part of the current code, where IFs are not implemented yet:

<td><?php echo date("D, d M Y", strtotime($val['date'])); ?></td>
<td><?php echo $val['firstName']." ".$val['lastName']; ?></td>
<td align="center"><?php echo $val['city'].", ".$val['state']; ?></td>
<td bgcolor="<?= $bg ?>" align="center"><?php echo $val['status']; ?></td>
<td align="center"><?php echo strtoupper($val['source']); ?></td>

Advertisement

Answer

Good day,

Structure wise (as in readability) it’s the best – at least I’m more comfortable with the ifs in-line, for example:

echo $queryResult->data;
echo '</td><td>';
if($queryResult->source == 'test'){
    echo 'Get info from other DB..';
}else{
    echo $queryResult->source;

echo '</td>'

It’s more readable to me, as you can just “descend” from the parent if. Whereas including all the content of the table in both the THEN-Block and also the ELSE-Block seems just redundant and hard to read.

However you should probably have a look at SQL Joins and Subqueries (assuming you’re using SQL) so you can avoid another query for every table row where the source is XY. This will turn into a performance disaster some time soon.

greets.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement