搜索不工作并在Codeigniter中获取错误的结果

时间:2017-07-12 04:15:35

标签: codeigniter

搜索不起作用并检索错误的结果

如果我只使用用户名进行搜索,但是当我使用用户名,客户和日期进行搜索时结果不会错误

我的模特

public function search_count($username,$customer,$datefrom,$dateend) {
    $this->db->where('t_u_id',$username);
    $this->db->where('t_customer_id',$customer);
    $this->db->where('t_date <=',$dateend);
    $this->db->where('t_date >=',$datefrom);
    return $this->db->count_all("transactions");
}
public function search($username,$customer,$datefrom,$dateend,$limit,$start)
{
    $start=$start-1;
    $this->db->select('*');
    $this->db->from('transactions');
    $this->db->where('t_u_id',$username);
    $this->db->where('t_customer_id',$customer);
    $this->db->where('t_date <=',$dateend);
    $this->db->where('t_date >=',$datefrom);
    $this->db->limit($limit,$start);
    $query=$this->db->get();
    if($query->num_rows() > 0){
        return $query->result();
    }else{
        return $query->result();
    }
}

我的控制器

    public function search($page_num = 1){
    $this->load->library('pagination');
    $this->load->helper("url");
    $this->load->model(array('CustomReport_m','user','Customer_m')); // This array to save number of lines only
    $username=$this->input->post('select_user');
    $username = ($this->uri->segment(3)) ? $this->uri->segment(3) : $username;
    $customer=$this->input->post('select_customer');
    $customer = ($this->uri->segment(4)) ? $this->uri->segment(4) : $customer;
    $datefrom=$this->input->post('from');
    $datefrom = ($this->uri->segment(5)) ? $this->uri->segment(5) : $datefrom;
    $dateend=$this->input->post('to');
    $dateend = ($this->uri->segment(6)) ? $this->uri->segment(6) : $dateend;
    $total_row = $this->CustomReport_m->search_count($username,$customer,$datefrom,$dateend);
    //$config['use_page_numbers'] = TRUE;
    $config['base_url'] = site_url("CustomReport/search/$username/$customer/$datefrom/$dateend");
    $config['total_rows'] = $total_row;
    $config['per_page'] = 10;
    $choice = $config["total_rows"]/$config["per_page"];
    $config["num_links"] = floor($choice);
    $config['full_tag_open'] = '<ul class="pagination">';
    $config['full_tag_close'] = '</ul>';
    $config['first_link'] = false;
    $config['last_link'] = false;
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['prev_link'] = '&laquo';
    $config['prev_tag_open'] = '<li class="prev">';
    $config['prev_tag_close'] = '</li>';
    $config['next_link'] = '&raquo';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="active"><a href="#">';
    $config['cur_tag_close'] = '</a></li>';
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';
    $this->pagination->initialize($config);
    if ($this->uri->segment(7)) {
        $page = ($this->uri->segment(7));
    } else {
        $page = 1;
    }
    $str_links = $this->pagination->create_links();
    $data["links"]= explode('&nbsp;', $str_links);
    $data['users']=$this->user->display_users();
    $data['userdata'] = $this->user->userdata();
    $data['customers']= $this->Customer_m->index();
   if(isset($username)and !empty($username)){
       $data["rsl"] =$this->CustomReport_m->search($username,$customer,$datefrom,$dateend,$config["per_page"],$page);
        $this->load->view('customreport_v',$data);
    }else{
        redirect('CustomReport','refresh');
    }
}

1 个答案:

答案 0 :(得分:0)

我认为你应该使用它:

    public function search_count($username,$customer,$datefrom,$dateend) {
        $this->db->select('*');
        $this->db->from('transactions');
        $this->db->where('t_u_id', $username);
        $this->db->where('t_customer_id', $customer);
        $this->db->where('t_date <=', $dateend);
        $this->db->where('t_date >=', $datefrom);
        return $this->db->get()->num_rows();
    }

因为count_all函数只返回行总数,所以它不能应用于哪里。