2013-08-21 99 views
0

我想在我的搜索表單中創建codeigniter分頁,當我點擊提交時,它成功顯示查詢搜索,但是當我點擊數字頁面鏈接或下一頁時,它顯示消息「未定義變量rec」和「致命錯誤:調用一個非對象的成員函數num_rows()」,我相信查詢已經傳遞給變量$ rec。帶查詢搜索的分頁codeigniter

而我在這裏看到問題時,我改變頁面,變量返回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"); 
} 
} 

回答

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(); 
+0

你也可以生成用於分頁搜索和在thephpcode.com其他CRUD操作的代碼。需要爲每個表添加模塊,然後可以爲函數添加字段和操作。 – Nishanthan

+0

我是codeigniter的新手,你能解釋一下你的代碼嗎? –

+0

$ condition是一個包含字段的列和值的數組。請讓我知道你不明白哪一步。 searchPage是我模型中的一種方法。 – Nishanthan