分页codeigniter与查询搜索

时间:2013-08-21 02:57:50

标签: codeigniter pagination

我想在我的搜索表单中创建codeigniter分页,当我点击提交时,它成功显示查询搜索但是当我点击数字页面链接或下一页时,它显示消息“未定义变量rec”和“致命错误:在非对象上调用成员函数num_rows()“,我确定查询已传递给变量$ rec。

我在这里看到问题,当我更改页面时,变量返回null,因此变量$ rec没有从'post'获得值。

我的问题:如何保持“发布”的值,以便在我更改页面时查询将获得变量值?

这是我的控制器:

public function search(){

$kat=$this->input->post('kategori');
$prov=$this->input->post('prov');
$kot=$this->input->post('kota');


if($kat=='' && $prov!='' && $kot!=''){
$rec=$this->db->query("select * from showall where id_prov='$prov' and id_kota='$kot' ");
}
else if($kot=='' && $kat!='' && $prov!=''){
$rec=$this->db->query("select * from showall where id_kat='$kat' and id_prov='$prov' ");
}
else if($kat=='' && $kot=='' && $prov!=''){
$rec=$this->db->query("select * from showall where id_prov='$prov' ");
}
else if($kat!='' && $prov=='' && $kot==''){
$rec=$this->db->query("select * from showall where id_kat='$kat' ");
}
else if($kat!='' && $prov!='' && $kot!=''){
$rec=$this->db->query("select * from showall where id_kat='$kat' and id_prov='$prov' and id_kota='$kot' ");
}

$dat=$rec;
$data['count']=$dat->num_rows();
  if($data['count'] >0){
  $data['db']=$rec;
$config['total_rows'] = $data['db']->num_rows(); 

$config['base_url'] = base_url().'index.php/lowongan/cari';

$config['per_page'] = 1;    
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['paging'] = $this->pagination->create_links();
if($kat=='' && $prov!='' && $kot!=''){
$d = $this->db->get_where('showall',array('id_prov'=>$prov,'id_kota'=>$kot) ,$config['per_page'], $this->uri->segment(3));
}
else if($kot=='' && $kat!='' && $prov!=''){
$d= $this->db->get_where('showall',array('id_kat'=>$kat,'id_prov'=>$prov) ,$config['per_page'], $this->uri->segment(3));
}
else if($kat=='' && $kot=='' && $prov!=''){
$d = $this->db->get_where('showall',array('id_prov'=>$prov) ,$config['per_page'], $this->uri->segment(3));
}
 if($kat!='' && $prov=='' && $kot==''){
$d = $this->db->get_where('showall',array('id_kat'=>$kat) ,$config['per_page'], $this->uri->segment(3));
}
else if($kat!='' && $prov!='' && $kot!=''){
$d = $this->db->get_where('showall',array('id_kat'=>$kat,'id_prov'=>$prov,'id_kota'=>$kot) ,$config['per_page'], $this->uri->segment(3));
}

$data['record']=$d;
$data1['kat']=$this->query->kategoriAll();
$data1['prov']=$this->query->showProv();
$data['q1']=$this->query->lokasi();
$data['q2']=$this->query->perusahaan();
$data['q3']=$this->query->kategori();

$this->load->view("user/head");
$this->load->view("user/bar",$data1);   
$this->load->view( 'user/hal_cari',$data);
$this->load->view("user/footer");
}
else{
$data1['kat']=$this->query->kategoriAll();
$data1['prov']=$this->query->showProv();
$data['q1']=$this->query->lokasi();
$data['q2']=$this->query->perusahaan();
$data['q3']=$this->query->kategori();

$this->load->view("user/head");
$this->load->view("user/bar",$data1);   
$this->load->view( 'user/kosong',$data);
$this->load->view("user/footer");
}
}

1 个答案:

答案 0 :(得分:0)

您需要使用assoc_to_uri()将条件数组保留在URL中。我在这样做。

    $this->load->model('classified_model','classified');
    $ClassifiedList = $this->classified->searchPage($condition,'classified_view',$perPage,$page*$perPage);
    $uriString = $this->uri->assoc_to_uri($condition);
    $config['uri_segment'] = count($condition) * 2 + 4;
    $base_url = site_url('classified/search/' . $uriString . '/page/');
    $base_url = preg_replace("/\.[a-z]*$/","", $base_url);
    $config['base_url'] = $base_url;
    $config['per_page'] = 1;

再次使用

从uri获取$条件
   $condition = $this->uri->uri_to_assoc();