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

How do I get the value of the sequential number of 2 pieces foreach function in codeigniter?

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

中获取序号的值

我使用这个代码:

    $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

更改您的代码

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

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

说,
客户 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 ###


希望这能解决您的问题。