dqx13503925528 2013-12-10 14:59
浏览 30
已采纳

单个IF语句与从SQL表中获取的数组中的多个IF语句[关闭]

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>
  • 写回答

1条回答 默认 最新

  • dongwuwan5646 2013-12-10 15:07
    关注

    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 propably 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向