在Model PHP CI中获取结果搜索

时间:2019-03-22 09:51:10

标签: php codeigniter

你好,我的查询模型有疑问。

这是我的控制器:

    if($this->input->post('journalsearch'))
    {
        //VARIABLE SEARCH
        $start = $this->input->post('start');
        $limit = $this->input->post('limit');
        $dir = $this->input->post('dir');
        $order = $this->input->post('sort');
        $key = $this->input->post('journalsearch');
        $start_date = date('Y-m-d',strtotime($date_awal));
        $end_date = date('Y-m-d',strtotime($date_akhir));
        $journalType = $this->input->post('journal_type');
        $unposted = $this->input->post('unposted');
        $posted = $this->input->post('posted');
        //END VARIABLE SEARCH


        $q = $this->_model->searchJournal($key,$start,$limit,$dir,$order,$start_date,$end_date,$journalType,$posted,$unposted);

        $i=0;
        foreach($q as $row=>$value)
        {
            switch($value['JOURNAL_IS_POSTED'])
            {
                case '0' :
                    $q[$i]['JOURNAL_IS_POSTED'] = 'No';
                    break;
                case '1' :
                    $q[$i]['JOURNAL_IS_POSTED'] = 'Yes';
                    break;
                default:
                    $q[$i]['JOURNAL_IS_POSTED'] = '';
            }

            $i++;
        }

        $data['total'] = $this->_model->juml_search($this->input->post('journalsearch'),$jurnal_type);
        $data['result'] = $q;
    }

这是我的模型:

function searchJournal($key,$start,$limit,$dir,$order,$start_date,$end_date,$journalType,$posted,$unposted)
{
    $this->db->select('JOURNAL_POSTED_DATE,JOURNAL_MEMO,JOURNAL_NUMBER,JOURNAL_TYPE_CODE,t_journal.JOURNAL_ID as JOURNAL_ID,JOURNAL_IS_POSTED,DATE_FORMAT(JOURNAL_DATE,\'%d %M %Y\') as JOURNAL_DATE,sum(if(JOURNAL_DETAIL_TYPE=1,JOURNAL_DETAIL_SUM,0)) as Debet,sum(if(JOURNAL_DETAIL_TYPE=2,JOURNAL_DETAIL_SUM,0)) as Kredit',false);
    $this->db->join('t_journal_detail','t_journal_detail.JOURNAL_ID=t_journal.JOURNAL_ID','left');

    MY_Model::branch_filtering($this,'t_journal','JOURNAL_CREATED_BY');//(object,'nama tabel atau alias','nama kolom')

    if($key AND $posted == true)
    {
        $this->db->like('t_journal.JOURNAL_ID',$key);
        $this->db->or_like('JOURNAL_TYPE_CODE',$key);
        $this->db->or_like('JOURNAL_NUMBER',$key);
        $this->db->or_like('JOURNAL_DATE',$key);
        $this->db->or_like('JOURNAL_MEMO',$key);
        $this->db->or_like('JOURNAL_CREATED_BY',$key);
        $this->db->or_like('JOURNAL_CREATED_DATE',$key);
        $this->db->or_like('JOURNAL_LAST_UPDATE_BY',$key);
        $this->db->or_like('JOURNAL_LAST_UPDATE',$key);
        $this->db->where('t_journal.JOURNAL_TYPE_CODE',$journalType);
        $this->db->where('JOURNAL_IS_POSTED',1);
        $this->db->where('DATE_FORMAT(JOURNAL_DATE,\'%Y-%m-%d\') >=',$start_date);
        $this->db->where('DATE_FORMAT(JOURNAL_DATE,\'%Y-%m-%d\') <=',$end_date);
    }
    elseif($key AND $unposted == true)
    {
        $this->db->like('t_journal.JOURNAL_ID',$key);
        $this->db->or_like('JOURNAL_TYPE_CODE',$key);
        $this->db->or_like('JOURNAL_NUMBER',$key);
        $this->db->or_like('JOURNAL_DATE',$key);
        $this->db->or_like('JOURNAL_MEMO',$key);
        $this->db->or_like('JOURNAL_CREATED_BY',$key);
        $this->db->or_like('JOURNAL_CREATED_DATE',$key);
        $this->db->or_like('JOURNAL_LAST_UPDATE_BY',$key);
        $this->db->or_like('JOURNAL_LAST_UPDATE',$key);
        $this->db->where('t_journal.JOURNAL_TYPE_CODE',$journalType);
        $this->db->where('JOURNAL_IS_POSTED',0);
        $this->db->where('DATE_FORMAT(JOURNAL_DATE,\'%Y-%m-%d\') >=',$start_date);
        $this->db->where('DATE_FORMAT(JOURNAL_DATE,\'%Y-%m-%d\') <=',$end_date);
    }

    $this->db->limit($limit,$start);
    $this->db->order_by($order,$dir);
    $this->db->group_by('JOURNAL_ID');

    $q = $this->db->get('t_journal');
    $result = $q->result_array();
    $q->free_result();
    return $result;
}

我试图建立这样的模型,但是结果却不是我想要的。

我想要查询的结果,例如:我以状态为条件,日期范围为2019-02-01至2019-02-28的方式搜索关键字日记帐编号“ 000798”输入保费收据日记。

然后结果

journal number like = 000798
where journal_is_posted = 1
and journal_type = premium receipt journal
and date_format (journal_date, \ '% Y-% m-% d \') >= ', 2019-02-01
and date_format (journal_date, \ '% Y-% m-% d \') <= ', 2019-02-28

求我的朋友们,我需要一个解决方案,谢谢。

0 个答案:

没有答案