如何删除codeigniter中的重复值数组?

我想在我的应用程序中制作 PDF 打印报告,我使用 codeigniter。这是我的桌子


stock

-----------------------------------------------------

id_stock  code_brg   nama_brg       category_id_stock

-----------------------------------------------------

  1       CODE 01    BEANS RAKYAT         1

  2       CODE 02    BEANS BLEND HOT      1

  3       CODE 03    BEANS BLEND ICE      1

kategori

-----------------------------------------------------

id   code_kategori  kategori_brg

-----------------------------------------------------

 1      code01         BEANS

 2      code02         SYRUP

 3      code03         MILK

 4      code04         TOOLS

这个我的控制器


    public function print()

    {

        $this->load->library('dompdf_gen');

        $stock = $this->Model_Stock;

        $tgl_awal = $this->input->post('tgl_awal');

        $tgl_akhir = $this->input->post('tgl_akhir');

        $data["report"] = $stock->cetak($tgl_awal, $tgl_akhir);

        $result = array(

            'tgl_awal' => $tgl_awal,

            'tgl_akhir' => $tgl_akhir

        );

        $arr = array_merge($data,$result);

        $this->load->view('stock/report_all',$arr);

    }

这个我的模型


    public function cetak($tgl_awal, $tgl_akhir)

    {

        $this->db->select('*');

        $this->db->from($this->_table);

        $this->db->join('kategori_brg', 'kategori_brg.id = stock.category_id_stock');

        $this->db->join('satuan_brg', 'satuan_brg.id = stock.satuan_id');

        $this->db->where('tgl_entri >=',$tgl_awal);

        $this->db->where('tgl_entri <=',$tgl_akhir);

        $query = $this->db->get()->result_array();

        return $query;

    }

这个我的观点


    <table class="table" style="width: 100%;">

        <?php

            $no= 1;

            foreach($report as $key){

        ?>

        <tr>

            <td class="td_table" style="text-align: center;"><?= $key['kategori_brg']?></td>

        </tr>

我怎么解决这个问题?


叮当猫咪
浏览 72回答 2
2回答

繁星淼淼

您可以在类别字段中使用ORDER BY,以便将具有相同类别的所有数据分组在一起,然后在您的类别中,foreach您可以将push每个类别放在一个中array并根据需要显示数据,如下所示 -模型public function cetak($tgl_awal, $tgl_akhir){&nbsp; &nbsp; $this->db->select('*');&nbsp; &nbsp; $this->db->from($this->_table);&nbsp; &nbsp; $this->db->join('kategori_brg', 'kategori_brg.id = stock.category_id_stock');&nbsp; &nbsp; $this->db->join('satuan_brg', 'satuan_brg.id = stock.satuan_id');&nbsp; &nbsp; $this->db->where('tgl_entri >=',$tgl_awal);&nbsp; &nbsp; $this->db->where('tgl_entri <=',$tgl_akhir);&nbsp; &nbsp; $this->db->order_by("kategori_brg", "ASC"); // ORDER BY kategori_brg&nbsp;&nbsp; &nbsp; $query = $this->db->get()->result_array();&nbsp; &nbsp; return $query;}&nbsp;看法<table class="table" style="width: 100%;">&nbsp; &nbsp;&nbsp;<?php&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$no = 1;$category = array(); // initialize array which will contain the categories&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;foreach($report as $key){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if(!in_array($key['kategori_brg'], $category)){ // check if category exists in the array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $category[] = $key['kategori_brg']; // insert the value in the array and show it as a heading?>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $key['kategori_brg']?></td>&nbsp; &nbsp; &nbsp; &nbsp; </tr><?php&nbsp; &nbsp; }&nbsp;?>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $no++ .''?></td>&nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $key['code_brg']?></td>&nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $key['nama_brg']?></td>&nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $key['satuan_pcs']?></td>&nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $key['stock_awal']?></td>&nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $key['stock_masuk']?></td>&nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $key['stock_keluar']?></td>&nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $key['stock_sisa']?></td>&nbsp; &nbsp; </tr>&nbsp; &nbsp;&nbsp;<?php}&nbsp; &nbsp;&nbsp;?></table>希望对您有帮助。

守着一只汪

看看这是否有效。将您的视图更改为:&nbsp; &nbsp; <table class="table" style="width: 100%;">&nbsp; &nbsp; <?php&nbsp; &nbsp; $no= 1;&nbsp; //store categories in array&nbsp; &nbsp; $kategori_brg_arr=array();&nbsp; &nbsp; foreach($report as $key)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; array_push($kategori_brg_arr,$key['kategori_brg']);&nbsp; &nbsp; }&nbsp; &nbsp; $kategori_brg_arr=array_unique($kategori_brg_arr);&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; //group categories&nbsp; &nbsp; foreach($kategori_brg_arr as $kategori_brg){&nbsp; &nbsp; &nbsp; foreach($report as $key){&nbsp; &nbsp; &nbsp; &nbsp; if($key['kategori_brg']==$kategori_brg){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $key['kategori_brg']?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $no++ .''?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $key['code_brg']?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $key['nama_brg']?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $key['satuan_pcs']?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $key['stock_awal']?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $key['stock_masuk']?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $key['stock_keluar']?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td class="td_table" style="text-align: center;"><?= $key['stock_sisa']?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; ?>&nbsp;&nbsp;&nbsp; </table>
打开App,查看更多内容
随时随地看视频慕课网APP