drol55885602 2016-06-13 06:49
浏览 309
已采纳

Foreach循环里面有两个查询

I have two queries which is working fine and it would retrieve 3 rows each query. I am trying to display a query result on a table using foreach with two(2) queries inside a foreach loop. I tried putting two result() on foreach but its an error. How can i display a two result() on a single foreach loop? I don't know how can i achieve this.

Query 1 would be on column "Investor Name" then query 2 will be on "Amount".

Here is the code:

<?php

    $query5 = $this->db->query("SELECT * FROM ".tbl_investors." WHERE id IN (SELECT MAX(investor_id) FROM ".tbl_investors_ledger." GROUP BY investor_id ) AND deleted = 0");

    $query6 = $this->db->query("SELECT * FROM ".tbl_investors_ledger." WHERE id IN (SELECT MAX(id) FROM ".tbl_investors_ledger." GROUP BY investor_id ) AND deleted = 0");

?>

                    <table class="table table-striped table-bordered table-hover" id="dataTables">
                        <thead>
                            <tr>  
                                <td>Investor Name</td>
                                <td>Amount</td>
                          </tr>
                        </thead>
                        <tbody style="text-align: center;">

                                <?php
                                foreach ($query5->result() as $row) && ($query6->result() as $row2){
                                ?>
                                <tr>
                                    <td><?php echo $row->last_name.', '.$row->first_name; ?></td>
                                    <td><?php echo $row2->amount; ?></td>
                                </tr>
                                <?php } ?>

                        </tbody>
                    </table>
  • 写回答

2条回答 默认 最新

  • dqudtskm49788 2016-06-13 07:12
    关注

    You can't use two array_expressions on foreach loop. It is better to use join on your query to make it one. something like

    $query = "SELECT `tbl_investors`.* , `tbl_investors_ledger`.*  
     FROM `tbl_investors` 
     LEFT JOIN  `tbl_investors_ledger` 
     ON `tbl_investors`.id = `tbl_investors_ledger`. investor_id 
     WHERE `tbl_investors`.deleted = 0 AND `tbl_investors_ledger`.deleted = 0 
     GROUP BY `tbl_investors_ledger`.investor_id 
     ORDER BY `tbl_investors_ledger`.id DESC ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来