我正在开发这个codeigniter项目。我正在显示一些问题,我想使用分页来批量显示问题。首次加载时一切正常(我的意思是在第一页)但是当我导航到下一页时,它无法正常工作。所以我意识到表单输入值不会保留在后续页面上,所以我决定使用session来保存这些表单值,但它仍然没有被保留。我的控制器:
public function displayQuestions()
{
$sessData = [
'classLevel' => $this->input->post('class_level'),
'subject' => $this->input->post('subject')
];
$this->session->set_userdata($sessData);
$config = array();
$page = (($this->uri->segment(3)) ? $this->uri->segment(3) : 0);
$config["base_url"] = base_url().'questions/displayQuestions';
$config["total_rows"] = 20;
$config["per_page"] = 5;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$data["links"] = $this->pagination->create_links();
// print_r($this->session->classLevel); exit;
$data['questions'] = $this->questions_model->selectQuestions($this->session->classLevel, $this->session->subject, $config["per_page"], $page);
// $this->load->view('layouts/header');
$this->load->view('users/questions', $data);
// $this->load->view('layouts/sidebar');
// $this->load->view('layouts/footer');
}
注意:我使用表单输入值来查询数据库以显示问题。我的模特:
public function selectQuestions($classLevel, $subject, $limit, $start)
{
$questions = $this->db->where('class_level', $classLevel)
->where('subj_code', $subject)
// ->order_by('id', 'RANDOM')
->limit($limit, $start)
->get('questions');
// $questions = $this->db->limit($limit, $start)
// ->get('questions');
if ($questions->num_rows() > 0) {
return $questions->result_array();
// print_r($questions->result_array());
}
return false;
}
答案 0 :(得分:1)
当您导航到下一页(不包含任何POST数据)时,我猜您正在清除全部或部分会话数据。尝试更改部分
$sessData = [
'classLevel' => $this->input->post('class_level'),
'subject' => $this->input->post('subject')
];
$this->session->set_userdata($sessData);
要:
if ($this->input->post('class_level') && $this->input->post('subject')):
$sessData = [
'classLevel' => $this->input->post('class_level'),
'subject' => $this->input->post('subject')
];
$this->session->set_userdata($sessData);
endif;
意味着您只在先前发布数据时才设置会话。