如何获得codeigniter中每个函数的2个序列号的值?

t0ybt7op  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(494)

我的代码有问题,因为我使用嵌套的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 ###

但我得到一个这样的数字,看文档结果的左边

yqyhoc1h

yqyhoc1h1#

更改代码

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

7jmck4yq2#

我不知道这是不是你想要的,但你想显示项目的数字顺序?
说,
客户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 ###

希望这能解决你的问题。

相关问题