如何在codeigniter中获取2个foreach函数序列号的值?

时间:2018-04-23 08:33:05

标签: php mysql codeigniter nested-loops html2pdf

我的代码有问题,因为我使用嵌套的foreach从我的数据库中获取一些值。 我需要在表格中获取序号的值。

我使用此代码:

    $TipeIklan          = $this->input->post('inputTipeIklan');
    $ProdukCode     = $this->input->post('inputPtodukCode');
    $Date               = $this->input->post('inputDate');
    $WherCustomer       = array("CustomerCode >="       => $Customer1,
                            "CustomerCode <="       => $Customer2);
    $ArrCustomer        = $this->customer_m->order_by("CustomerCode","ASC")->get_many_by($WherCustomer);

    if($ArrCustomer){
        foreach($ArrCustomer as $item_customer){
            $Kwitansi   = $this->kwitansi_m->getUmurPiutang($item_customer->CustomerCode,$ProdukCode,$Date);
            if($Kwitansi){
                $content .='
                            <tr>
                                <td class="fontB" colspan="17">'.$item_customer->CustomerCode.'</td>
                            </tr>';

                foreach($Kwitansi as $item){
                    $n  = count($Kwitansi);
                    $no = $n;
                    $content .='
                                <tr>
                                    <td class="font" width="200">'.$no.'</td>
                                </tr>';
                    $n++;           
                } ### end of foreach $Kwitansi as $item ###
            } ### end of $Kwitansi ###
        } ### end of foreach $ArrCustomer as $item_customer ###
    } ### end of $ArrCustomer ###

但我得到这样的数字, 看一下文档的左侧 RESULT

2 个答案:

答案 0 :(得分:1)

更改您的代码

foreach($Kwitansi as $item){
        $n  = count($Kwitansi);
        $no = $n;
        $content .='
                            <tr>
                                <td class="font" width="200">'.$no.'</td>
                            </tr>';
        $n++;

}

$m  = 0;
foreach($Kwitansi as $item){
        $n = $m+1;
        $content .='
                            <tr>
                                <td class="font" width="200">'.$no.'</td>
                            </tr>';
        $n++;
        $m++;
    }

 $n  = count($Kwitansi); this wrong because of if you got 5 count then start with 5,6,7...n. So, you need to put before loop and assign $n=1 to start with 1,2 .... so on

答案 1 :(得分:0)

我不确定这是否是您想要的,但是您想要按顺序显示这些项目的数字吗?

说,
客户X
1)项目A
客户Y
2)项目B
3)项目C
客户Z
4)项目D

如果是这样的话: 然后在$ ArrCustomer foreach之前初始化你的计数器并在$ Kwitansi foreach中增加它。

$TipeIklan          = $this->input->post('inputTipeIklan');
    $ProdukCode     = $this->input->post('inputPtodukCode');
    $Date               = $this->input->post('inputDate');
    $WherCustomer       = array("CustomerCode >="       => $Customer1,
                            "CustomerCode <="       => $Customer2);
    $ArrCustomer        = $this->customer_m->order_by("CustomerCode","ASC")->get_many_by($WherCustomer);
    $n = 1; // counter that never gets reset.

    if($ArrCustomer){
        foreach($ArrCustomer as $item_customer){
            $Kwitansi   = $this->kwitansi_m->getUmurPiutang($item_customer->CustomerCode,$ProdukCode,$Date);
            if($Kwitansi){
                $content .='
                            <tr>
                                <td class="fontB" colspan="17">'.$item_customer->CustomerCode.'</td>
                            </tr>';

                foreach($Kwitansi as $item){
                    $no = $n;
                    $content .='
                                <tr>
                                    <td class="font" width="200">'.$no.'</td>
                                </tr>';
                    $n++;           
                } ### end of foreach $Kwitansi as $item ###
            } ### end of $Kwitansi ###
        } ### end of foreach $ArrCustomer as $item_customer ###
    } ### end of $ArrCustomer ###


希望这能解决您的问题。