memfilter data yang bernilai '<0', '>0', dan '=0' pada php mysql framework codeigniter

Kodingan dibawah untuk sistem inventory. Total Stok barang ini hasil dari penambahan total dari setiap gudang (total ada 11 gudang), pada Total Stok barangnya ada yang bernilai plus dan minus. Saya mau memfilter pada bagian view agar bisa memilih barang mana yang memiliki nilai Total Stok yang plus (> 0), Minus (< 0), atau Nol (= 0)

Kodingan Controllernya

$product_name           = $this->input->get('product_name') ? $this->input->get('product_name') : null;
$product                = $this->input->get('product') ? $this->input->get('product') : null;
$stock                  = $this->input->get('stock') ? $this->input->get('stock') : null;

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

            // Subquery buat jumlah tiap wh
            $this->db->select('product_id, 
                MAX(CASE WHEN warehouse_id = 1 THEN quantity ELSE NULL END) AS whqty1,
                MAX(CASE WHEN warehouse_id = 2 THEN quantity ELSE NULL END) AS whqty2,
                MAX(CASE WHEN warehouse_id = 3 THEN quantity ELSE NULL END) AS whqty3,
                MAX(CASE WHEN warehouse_id = 4 THEN quantity ELSE NULL END) AS whqty4,
                MAX(CASE WHEN warehouse_id = 5 THEN quantity ELSE NULL END) AS whqty5,
                MAX(CASE WHEN warehouse_id = 6 THEN quantity ELSE NULL END) AS whqty6,
                MAX(CASE WHEN warehouse_id = 7 THEN quantity ELSE NULL END) AS whqty7,
                MAX(CASE WHEN warehouse_id = 8 THEN quantity ELSE NULL END) AS whqty8,
                MAX(CASE WHEN warehouse_id = 9 THEN quantity ELSE NULL END) AS whqty9,
                MAX(CASE WHEN warehouse_id = 10 THEN quantity ELSE NULL END) AS whqty10,
                MAX(CASE WHEN warehouse_id = 11 THEN quantity ELSE NULL END) AS whqty11',
                false)
                ->from('warehouses_products')
                ->group_by('product_id');
            $warehouse_quantities_subquery = $this->db->get_compiled_select();

            // Main query
            $this->datatables
                ->select("
                    {$this->db->dbprefix('products')}.id as productid, 
                    {$this->db->dbprefix('products')}.image as image, 
                    {$this->db->dbprefix('products')}.barcode_product as code, 
                    {$this->db->dbprefix('products')}.name as name,
                    {$this->db->dbprefix('products')}.second_name as packing,
                    {$this->db->dbprefix('products')}.product_status as product_status,                    
                    wq.whqty1, 
                    wq.whqty2,        
                    wq.whqty3,
                    wq.whqty4, 
                    wq.whqty5,
                    wq.whqty6,
                    wq.whqty7,
                    wq.whqty8,
                    wq.whqty9,
                    wq.whqty10,
                    wq.whqty11,
                    (COALESCE(wq.whqty1, 0) + COALESCE(wq.whqty2, 0) + COALESCE(wq.whqty3, 0) + COALESCE(wq.whqty4, 0) + COALESCE(wq.whqty5, 0) + COALESCE(wq.whqty6, 0) + COALESCE(wq.whqty7, 0) + COALESCE(wq.whqty8, 0) + COALESCE(wq.whqty9, 0) + COALESCE(wq.whqty10, 0) + COALESCE(wq.whqty11, 0))"     
            
        , false)
        ->from('products')
        ->join("($warehouse_quantities_subquery) wq", 'wq.product_id = products.id', 'left')
        ->join('users', 'products.created_by=users.id', 'left')
        ->join('companies', 'products.supplier1=companies.id', 'left')
        ->join('brands', 'products.brand=brands.id', 'left')            
        ->group_by('products.id');

        
        if ($product_name) {
            $this->datatables->like('products.name', $product_name, 'both');
        }
        if ($product) {
            $this->datatables->where('products.id', $product);
        }

        if ($stock == '>0') {
            $this->datatables->where('(COALESCE(wq.whqty1, 0) + COALESCE(wq.whqty2, 0) + COALESCE(wq.whqty3, 0) + COALESCE(wq.whqty4, 0) + COALESCE(wq.whqty5, 0) + COALESCE(wq.whqty6, 0) + COALESCE(wq.whqty7, 0) + COALESCE(wq.whqty8, 0) + COALESCE(wq.whqty9, 0) + COALESCE(wq.whqty10, 0) + COALESCE(wq.whqty11, 0)) > 0', null, false);
        } elseif ($stock == '<0') {
            $this->datatables->where('(COALESCE(wq.whqty1, 0) + COALESCE(wq.whqty2, 0) + COALESCE(wq.whqty3, 0) + COALESCE(wq.whqty4, 0) + COALESCE(wq.whqty5, 0) + COALESCE(wq.whqty6, 0) + COALESCE(wq.whqty7, 0) + COALESCE(wq.whqty8, 0) + COALESCE(wq.whqty9, 0) + COALESCE(wq.whqty10, 0) + COALESCE(wq.whqty11, 0)) < 0', null, false);
        } elseif ($stock == '=0') {
            $this->datatables->where('(COALESCE(wq.whqty1, 0) + COALESCE(wq.whqty2, 0) + COALESCE(wq.whqty3, 0) + COALESCE(wq.whqty4, 0) + COALESCE(wq.whqty5, 0) + COALESCE(wq.whqty6, 0) + COALESCE(wq.whqty7, 0) + COALESCE(wq.whqty8, 0) + COALESCE(wq.whqty9, 0) + COALESCE(wq.whqty10, 0) + COALESCE(wq.whqty11, 0)) = 0', null, false);
        }       
        
        } 
        echo $this->datatables->generate();

Dibawah ini Kodingan Viewnya

<?php
    $v = '';
    if ($this->input->post('product_name')) {
        $v .= '&product_name=' . $this->input->post('product_name');
    }
    if ($this->input->post('product')) {
        $v .= '&product=' . $this->input->post('product');
    }
    
    if($this->input->post('stock')){
        $v .= "&stock =" .$this->input->post('stock');
    }
    
?>

<div class="col-sm-6">
    <div class="form-group">
        <label class="control-label" for="stock"><?= lang('Stock Filter'); ?></label>
        <select name="stock" id="stock" class="form-control">
            <option value=""><?= lang('Select'); ?></option>
            <option value=">0" <?= (isset($_POST['stock']) && $_POST['stock'] == '>0') ? 'selected' : '' ?>><?= lang('Greater than 0'); ?></option>
            <option value="<0" <?= (isset($_POST['stock']) && $_POST['stock'] == '<0') ? 'selected' : '' ?>><?= lang('Less than 0'); ?></option>
            <option value="=0" <?= (isset($_POST['stock']) && $_POST['stock'] == '=0') ? 'selected' : '' ?>><?= lang('Equal to 0'); ?></option>
        </select>
    </div>
</div>

hasil dari kodingan diatas, tidak memfilter data sama sekali, saya sudah pakai fitur switch dan case juga sama saja. Tolong bantuannya dimana letak masalah dan perbaikannya. Terima kasih

avatar leoniisabell23
@leoniisabell23

2 Kontribusi 0 Poin

Diperbarui 2 bulan yang lalu

1 Jawaban:

Jawaban Terpilih

bisa coba dulu ini,,,

hilangkan spasi di $v .= "&stock =" menjadi $v .= "&stock=" karena ini pake encoding ntar valuenya jadi stock%20 (bukan stock). cek apakah bisa atau tidak,,, kalo tidak bisa,, coba pastikan penanganan untuk $_GET benar,,,

jika diperlukan,, mungkin bisa menggunakan debugging, supaya ketahuan udah sesuai ato belom log_message('debug', 'Stock Filter: ' . $stock);

avatar yukaristel
@yukaristel

35 Kontribusi 32 Poin

Dipost 2 bulan yang lalu

Tanggapan

iya benar ternyata gara2 spasi di "&stock ", thankk youuu bantuannya

bisa tolong di "jawaban benar" kan ? ? ?

oh sorry baru pertama kali di forum ini. done yah. thank youu

okey,,, gl dengan projectnya

Login untuk ikut Jawaban