|
本帖最后由 CI大菜鸟 于 2015-2-12 12:27 编辑
这个我是问了很多人,最后还是我的一个老师教我的,在网上看了很多,不过要不就看不明白,要不就运行了都没效果,也许是我菜吧,所以今天也发表一下我的代码。对于我们这些新手也许是简单的方法,我这个是一个下拉框分类提交查询,和搜索框查询做法都一样:
Controllers-控制器:
//分页配置
public function _page($url,$total,$seg=3){
$this->load->library('pagination');//开启分页类。
$config['uri_segment']=$seg;
$config['page_query_string']=TRUE;//开启重写url段,会自动加上&per_page,这可看手册。
$config['use_page_numbers'] = TRUE;//开启url中显示的页码数
$config['query_string_segment'] = 'page';//上面url追加的&per_page,可自定义修改单词。这个个人喜欢
$config['base_url'] = $url;//url地址调用到上面显示视图的方法
$config['total_rows'] = $total;//总记录数
$config['per_page'] = 5; //限制分页记录数
//这里可以不用管,只是自定义样式
$config['full_tag_open'] = '<div class="pagination"><ul>';
$config['prev_link'] = '上一页';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['next_link'] = '下一页';
$config['full_tag_close'] = '</ul></div>';
//$this->pagination->initialize($config);
return $this->pagination->create_links();
}
显示视图方法:
public function select_cat(){
$this->load->model('product_model');//model可以省略,我只是做数据查询
//查询分类
$data['cat_name'] = $this->db->get('product_cat')->result_array();
$pid = $this->input->get('cat_name');//接受get传来的值
$page = $this->input->get('page'); //接受在上面我定义好page
if(!$page){
$page=1; //第一页
}
$offset = ($page-1)*5;//5是显示条数
$data['pro_select'] = $this->product_model->select($offset,$pid);//这可看下面model
//总数量
$data['total'] = $this->db->where('pro_cat_id',$pid)->count_all_results('product');
//输出分页
$data['page'] = $this->_page(site_url('product/select_cat'.'?cat_name='.$pid),$data['total']);
$this->load->view('admin/header');
$this->load->view('admin/product',$data);
$this->load->view('admin/foot');
}
url显示的效果:第一页:控制器/方法?name名=get值
第二页:控制器/方法?name名=get值&page=页码数
model-模板:
public function select($offset,$pid){
$data = array('pro_cat_id'=>$pid,'wg_id'=>$pid);
return $pro_select = $this->db->or_where($data)->limit(5,$offset)->order_by('product_id','desc')->join('product_cat','cat_id = pro_cat_id')->get('product')->result_array();
}
view-视图:
<form action="<?php echo site_url('product/select_cat/')?>" method="get">
<select name="cat_name" id="" >
<option value="">查询分类...</option>
<?php foreach($cat_name as $name){?>
<option <?php if($name['cat_id']==$pid){ echo 'selected';}?> value="<?php echo $name['cat_id']?>" ><?php echo $name['cat_name'] ?></option>
<?php } ?>
</select>
<input type="submit" value="查询">
</form>
|
|