Duwi irwanto
Duwi irwanto

Reputation: 139

Get month and year with this->db->where on codeigniter

still problematic in my project . My own previous posts but no answer . I would like to call the Year and month of the mysql database of the results of the data POST.

This my controllers

public function report(){
$nomor = $this->input->post('nomor_reseller');
$bulan = $this->input->post('month'); // YYYY-MM

$report_data = $this->hasil_m->get_data($nomor, $bulan );
}

if only the month who called her work but if I enter the year and month did not work

This my models

function get_data($nomor,$bulan) {
    $this->db->select('*');
    $this->db->from('tb_pelanggan as pel');
    $this->db->join('tb_produk as pro', 'pel.id_barcode  = pro.id_barcode');    
    $this->db->select_sum('jumlah');
    $this->db->where('MONTH(pel.tgl_pembelian)',$bulan);
    $this->db->group_by('pel.id_barcode');
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        return $query->result();
        } else {
        return false;
    }
}   

Upvotes: 0

Views: 19415

Answers (2)

vijaykumar
vijaykumar

Reputation: 4806

Why don't you just use DATE_FORMAT instead of YEAR and MONTH

->where("DATE_FORMAT(column_name,'%Y-%m')", $bulan)

Upvotes: 3

Suhindra
Suhindra

Reputation: 355

first your controller

public function report(){
$nomor = $this->input->post('nomor_reseller');
$bulan = $this->input->post('month'); // MM
$tahun = $this->input->post('year'); // YYYY

$report_data = $this->hasil_m->get_data($nomor, $bulan, $tahun );
}

and your model

function get_data($nomor,$bulan,$tahun) {
    $this->db->select('*');
    $this->db->from('tb_pelanggan as pel');
    $this->db->join('tb_produk as pro', 'pel.id_barcode  = pro.id_barcode');    
    $this->db->select_sum('jumlah');
    $this->db->where('MONTH(pel.tgl_pembelian)',$bulan);
    $this->db->where('YEAR(pel.tgl_pembelian)',$tahun);
    $this->db->group_by('pel.id_barcode');
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        return $query->result();
        } else {
        return false;
    }
}   

Upvotes: 1

Related Questions