我们如何在编码点火器中添加总搜索结果计数和分页。
我想在结果页面上显示带有分页的总搜索结果计数。 谢谢 谁能帮忙
尝试了很多,但无法实现。
请帮忙
型
function search_results($title, $params = array()){
$this->db->select("ID,section,Title,img,year,lastupdateon AS ondate,'movies/' AS dept,STATUS");
$this->db->from('movies');
$this->db->Like('Title',$title,'after');
$this->db->or_Like('year',$title,'after');
$this->db->or_Like('section',$title,'after');
$query[] = $this->db->get_compiled_select();
$this->db->select("ID,section,Title,img,year,lastupdateon AS ondate,'view/kid/' AS dept,STATUS");
$this->db->from('kids');
$this->db->Like('Title',$title,'after');
$query[] = $this->db->get_compiled_select();
$this->db->select("ID,views,Title,img,version AS year,ondate,'view/software/' AS dept,STATUS");
$this->db->from('soft');
$this->db->Like('Title',$title,'after');
$query[] = $this->db->get_compiled_select();
$this->db->select("ID,section,Title,img,'PC Game' AS year,ondate,'view/game/' AS dept,STATUS");
$this->db->from('games');
$this->db->Like('Title',$title,'after');
$query[] = $this->db->get_compiled_select();
$this->db->select("ID,season,Title,img,type AS year,lastupdatedon AS ondate,'view/tvshow/' AS dept,STATUS");
$this->db->from('tvshows');
$this->db->Like('Title',$title,'after');
$query[] = $this->db->get_compiled_select();
$union_query = join(' UNION ALL ',$query); // I use UNION ALL
$union_query .= " ORDER BY ondate DESC";
if(array_key_exists("returnType",$params) && $params['returnType'] == 'count'){
$result = $this->$union_query->num_rows();
}
$query = $this->db->query($union_query);
//echo $query->num_rows();
return $query->result();
}
控制器
function search(){
$title=$this->input->get('Title');
$data['data']=$this->blog_model->search_results($title);
$config['per_page']= 10;
$config['uri_segment'] = 4;
$config['base_url'] = site_url('/index.php/tutorial/search').'/';
$this->data['per_page_display'] = $config['per_page'];
$conditions['returnType'] = 'count';
$totalRec = $this->blog_model->search_results($conditions);
$config['total_rows'] = count($conditions);
$this->data['totalcount'] = $config['total_rows'];
$this->pagination->initialize($config);
$this->data['paginglinks'] = $this->pagination->create_links();
$this->data['per_page'] = $this->uri->segment(4);
$this->data['page'] = $offset;
$this->load->view('search_view',$data);
}
问题 1:在控制器中,要获得总数,您必须将第一个参数作为标题传递
,然后条件$totalRec = $this->blog_model->search_results($title, $conditions);
问题 2:在模型中,最后一个 db 应该如下所示
$query = $this->db->query($union_query)->get();
if(array_key_exists("returnType",$params) && $params['returnType'] == 'count')
{
return $query->num_rows();
}
return $query->result();
请注意,我不明白分页背后的逻辑,但是您可以访问一次模型函数以获取数据并像下面一样计数
控制器:
$result = $this->blog_model->search_results($title);
$data['data'] = $result['data'];
$data['totalcount'] = $result['total'];
型:
$data = [];
$query = $this->db->query($union_query)->get();
$data['data'] = $query->num_rows();
$data['total'] = $query->result_array();
return $data;