Reputation: 13
I want to make a PDF print report in my application, I use codeigniter. this is my table
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
This MyController
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);
}
This MyModel
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;
}
This MyView
<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>
<tr>
<td class="td_table" style="text-align: center;"><?= $no++ .''?></td>
<td class="td_table" style="text-align: center;"><?= $key['code_brg']?></td>
<td class="td_table" style="text-align: center;"><?= $key['nama_brg']?></td>
<td class="td_table" style="text-align: center;"><?= $key['satuan_pcs']?></td>
<td class="td_table" style="text-align: center;"><?= $key['stock_awal']?></td>
<td class="td_table" style="text-align: center;"><?= $key['stock_masuk']?></td>
<td class="td_table" style="text-align: center;"><?= $key['stock_keluar']?></td>
<td class="td_table" style="text-align: center;"><?= $key['stock_sisa']?></td>
</tr>
<?php
}
?>
</table>
I want to print like this my expectations This my result my result, I tried using distinct, but it didn't work.
How can I solve this problem?
Upvotes: 1
Views: 1453
Reputation: 1961
You can use ORDER BY
on your category field so that all your data with the same category are grouped together, then in your foreach
you can push
each category in an array
and show the data as desired, like so -
Model
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);
$this->db->order_by("kategori_brg", "ASC"); // ORDER BY kategori_brg
$query = $this->db->get()->result_array();
return $query;
}
View
<table class="table" style="width: 100%;">
<?php
$no = 1;
$category = array(); // initialize array which will contain the categories
foreach($report as $key){
if(!in_array($key['kategori_brg'], $category)){ // check if category exists in the array
$category[] = $key['kategori_brg']; // insert the value in the array and show it as a heading
?>
<tr>
<td class="td_table" style="text-align: center;"><?= $key['kategori_brg']?></td>
</tr>
<?php
}
?>
<tr>
<td class="td_table" style="text-align: center;"><?= $no++ .''?></td>
<td class="td_table" style="text-align: center;"><?= $key['code_brg']?></td>
<td class="td_table" style="text-align: center;"><?= $key['nama_brg']?></td>
<td class="td_table" style="text-align: center;"><?= $key['satuan_pcs']?></td>
<td class="td_table" style="text-align: center;"><?= $key['stock_awal']?></td>
<td class="td_table" style="text-align: center;"><?= $key['stock_masuk']?></td>
<td class="td_table" style="text-align: center;"><?= $key['stock_keluar']?></td>
<td class="td_table" style="text-align: center;"><?= $key['stock_sisa']?></td>
</tr>
<?php
}
?>
</table>
Hope it helps you.
Upvotes: 1
Reputation: 435
See if this works. Change your view to:
<table class="table" style="width: 100%;">
<?php
$no= 1;
//store categories in array
$kategori_brg_arr=array();
foreach($report as $key)
{
array_push($kategori_brg_arr,$key['kategori_brg']);
}
$kategori_brg_arr=array_unique($kategori_brg_arr);
//group categories
foreach($kategori_brg_arr as $kategori_brg){
foreach($report as $key){
if($key['kategori_brg']==$kategori_brg){
?>
<tr>
<td class="td_table" style="text-align: center;"><?= $key['kategori_brg']?></td>
</tr>
<tr>
<td class="td_table" style="text-align: center;"><?= $no++ .''?></td>
<td class="td_table" style="text-align: center;"><?= $key['code_brg']?></td>
<td class="td_table" style="text-align: center;"><?= $key['nama_brg']?></td>
<td class="td_table" style="text-align: center;"><?= $key['satuan_pcs']?></td>
<td class="td_table" style="text-align: center;"><?= $key['stock_awal']?></td>
<td class="td_table" style="text-align: center;"><?= $key['stock_masuk']?></td>
<td class="td_table" style="text-align: center;"><?= $key['stock_keluar']?></td>
<td class="td_table" style="text-align: center;"><?= $key['stock_sisa']?></td>
</tr>
<?php
}
}
}
?>
</table>
Upvotes: 0