本帖最后由 helingjun2008 于 2010-4-5 13:39 编辑
看了很多的搜索的分页,很多的都是通过URL来传递要搜索的关键字,但是这样一来,就不好定位分页的页码到底是哪一个参数,由于本人水平有限,php的函数很多都不知道,于是想到了用session来传递,搜索的关键字,代码如下.
先是控制器,控制器很简单:
PHP复制代码 function Show_list ($data='no') {//显示列表
$this->load->model('Cangku_m');
$rowarray=$this->Cangku_m->cangkulist($data);
$links=$this->Cangku_m->links;
$this->load->view('show_list',array('rowarray'=>$rowarray,'links'=>$links,'title'=>'库存列表')); 复制代码 然后是模型:
PHP复制代码 function cangkulist ($data) {
if($data=='no') {
$data=array();
$str='/no';
$this->session->unset_userdata('seach');
}
if($this->input->post('seach')=='true') {
$data=array(
'id'=>$this->input->post('id'),
'pinpai'=>$this->input->post('pinpai'),
'xinghao'=>$this->input->post('xinghao'),
'shuliang'=>$this->input->post('shuliang'),
'shoujia'=>$this->input->post('shoujia'),
'cangku'=>$this->input->post('cangku'));
$this->session->set_userdata('seach',urlencode(json_encode($this->gbkenurl($data))));
$str='/seach';
}
if($data=='seach'){
$data=json_decode(urldecode($this->session->userdata('seach')),true);
$data=get_object_vars($data);
$data=$this->gbkdeurl($data);
$str='/seach';
}
foreach($data as $key=>$val) {
if(!$val='') {
if($key=='id') $this->db->where('id',$val) ;
if($key=='pinpai') $this->db->like('pinpai',$val) ;
if($key=='xinghao') $this->db->like('xinghao',$val) ;
if($key=='shuliang') $this->db->where('shuliang',$val) ;
if($key=='shoujia') $this->db->where('shoujia',$val) ;
if($key=='cangku') $this->db->where('cangku',$val) ;
}
}
$this->load->library('pagination');
$config['base_url']=site_url ('./cangku/show_list'.$str);
$this->db->from('cangku');
$config['total_rows']=$this->db->count_all_results();
$config['per_page']=20;
$config['uri_segment']=4;
$config['first_link']='首页';
$config['last_link']='末页';
$config['next_link']='下一页';
$config['prev_link']='上一页';
$this->pagination->initialize($config);
foreach($data as $key=>$val) {
if(!$val='') {
if($key=='id') $this->db->where('id',$val) ;
if($key=='pinpai') $this->db->like('pinpai',$val) ;
if($key=='xinghao') $this->db->like('xinghao',$val) ;
if($key=='shuliang') $this->db->where('shuliang',$val) ;
if($key=='shoujia') $this->db->where('shoujia',$val) ;
if($key=='cangku') $this->db->where('cangku',$val) ;
}
}
$this->db->order_by("id", "desc");
$query=$this->db->get('cangku',$config['per_page'],$this->uri->segment(4));
$this->links=$this->pagination->create_links();
Return $query->result_array();
}
function gbkenurl ($data) {
foreach($data as $key=>$val) {
$data[$key]=urlencode($val);
}
Return $data;
}
function gbkdeurl ($data) {
foreach($data as $key=>$val) {
$data[$key]=urldecode($val);
}
Return $data;
} 复制代码
由于服务器上用的是gbk的字符,而且是php4,所以要在把中文放入json之前做urlencode ,
大概的代码就是这样,另外在搜索的表单里面做一个,隐藏的seach字段,值为true,还有我的session是存在数据库里,方法虽然笨了点,但是,算解决了我的问题,
欢迎大家,指出其中的不足, |