foreach里面的foreach比在HTML表格中显示它

时间:2017-01-07 06:02:07

标签: php foreach

美好的一天。我现在正在尝试创建来自我的sql表的细节。

这是我的PHP: -

public function export()
    {
        $query = $this->input->cookie("cookie_invent_query");
        $data['header'] = $this->modelmodel->showdata($query);
        foreach($data['header'] as $header){
            $data['detail'][] = $this->modelmodel->showdata("select * from DeliveryOrderDetail 
                                                            where deliveryordertransno = '".$header->TransactionNo."' ");
        }
        echo "<pre>"; print_r($data['detail']);
        $this->load->view("do_mutasi/export",$data);
    }

上面的我的脚本我得到了print_r()

来自$data['header']

的结果
    Array
    (
        [0] => stdClass Object
            (
                [FinalReleaseStatus] => 1
                [DeliveryOrderDate] => 2016-12-21 17:25:18.487
                [TransactionNo] => DO-DL-K-LFKG-11
                [DocumentNo] => DOZZ-DL-K-LFKG-6
                [ToCustomerCode] => AFF004
                [CategoryCode] => ZZ
                [ETADate] => 2016-12-21 17:25:18.487
            )

    )

来自$ data ['detail']

的结果
Array
(
    [0] => Array
        (
            [0] => stdClass Object
                (
                    [TransactionNo] => DOD-DL-K-LFKG-9
                    [LineNo] => 1000
                    [ItemCode] => FA00000111
                    [DeliveryOrderTransNo] => DO-DL-K-LFKG-11
                    [ExtraRemark] => 0
                    [ExtraRemark2] => 0
                    [Quantity] => 3.00000000000000000000
                    [UOMCode] => PCS
                    [CreatedDate] => 2016-12-21 17:26:25.063
                    [CreatedBy] => boby
                    [ModifiedDate] => 2016-12-21 17:26:25.063
                    [ModifiedBy] => 
                )

        )

)

然后我发送到我的视图,这就是我现在可以做的事情

<?php foreach($header as $hdr) { ?>
 Header
    <table class="table">
        <thead>
            <tr>
                <th>Trnsaction No</th>
                <th>Document No</th>
                <th>To Customer</th>
                <th>Delivery Order Date</th>
            </tr>
        </thead>
        <tbody>
            <td><?=$hdr->TransactionNo;?></td>
            <td><?=$hdr->DocumentNo;?></td>
            <td><?=$hdr->ToCustomerCode;?></td>
            <td><?=$hdr->DeliveryOrderDate;?></td>
        </tbody>
    </table>
Detail
<table>
    <thead>
        <tr>
            <th>Transaction No</th>
            <th>Item Code</th>
            <th>Quntity</th>
            <th>Uom Code</th>
        </tr>
    </thead>
    <tbody>
     <?php 

     foreach($detail as $rsltdt =>$key) { ?>
        <tr>
            <td><?=$rsltdt['TransactionNo'];?></td>
            <td><?=$rsltdt['ItemCode'];?></td>
            <td><?=$rsltdt['Quantity'];?></td>
            <td><?=$rsltdt['UOMCode'];?></td>
        </tr>
    <?php } ?>
    </tbody>
</table>

<?php } ?>
你可以看到。我在detail循环中循环header。因为在某些records中有多个细节。我循环标题,因为我想要显示多个records而不只是一个record

这是迄今为止的结果

enter image description here

我无法显示细节。所以我的问题是。我如何显示细节取决于标题。

[TransactionNo] = [DeliveryOrderTransNo]

2 个答案:

答案 0 :(得分:1)

最好将所有细节放在相应的标题中:

public function export()
    {
        $query = $this->input->cookie("cookie_invent_query");
        $data['header'] = $this->modelmodel->showdata($query);
        foreach($data['header'] as $header){
            $data['header']['details'] = $this->modelmodel->showdata("select * from DeliveryOrderDetail 
                                                            where deliveryordertransno = '".$header->TransactionNo."' ");
        }
    }

然后你可以简单地编写第二个循环:

 foreach($hdr['details'] as $rsltdt) { ?>
    <tr>
        <td><?=$rsltdt->TransactionNo;?></td>
        <td><?=$rsltdt->ItemCode;?></td>
        <td><?=$rsltdt->Quantity;?></td>
        <td><?=$rsltdt->UOMCode;?></td>
    </tr>
<?php } ?>

您不再需要嵌套详细信息数组:

放:

$data['header']['details'] = $this->modelmodel->showdata("select * f....

而不是:

$data['header']['details'][] = $this->modelmodel->showdata("select * f....

答案 1 :(得分:1)

检查一次: -

foreach($detail as $rsltdt) { 
  foreach ($rsltdt as $rsl){ ?> 
    <tr> 
        <td><?=$rsl->TransactionNo;?></td> 
        <td><?=$rsl->ItemCode;?></td> 
        <td><?=$rsl->Quantity;?></td> 
        <td><?=$rsl->UOMCode;?></td> 
    </tr> 
<?php } } ?>