嗨,我是codeigniter的新手,并试图在我的项目中构建一个分页功能,看起来一切正常,除了分页不改变页面。嘲笑什么是错的,任何人都可以帮助我吗?这是我的代码:
控制器
class Result_controller extends CI_Controller{
function getall(){
$this->load->model('result_model');
$data['query'] = $this->result_model->result_getall();
// print_r($data['query']); die();
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/Surva/index.php/result_controller/getall';
$config['total_rows'] = $this->db->get('tblanswers, credentials')->num_rows();
$config['per_page'] = 1;
$config['num_links'] = 10;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data['records'] = $this->Result_model->result_getall($config['per_page'], $this->uri->segment(3));
$data['pagination'] = $this->pagination->create_links();
$this->load->view('result_view', $data);
模型
function result_getall(){
$this->db->select('tblanswers.*,credentials.*');
$this->db->from('tblanswers');
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'LEFT');
$query = $this->db->get();
return $query->result();
}
查看
<?php foreach ($query as $row): ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->second_name; ?></td>
<td><?php echo $row->phone; ?></td>
<td><?php echo $row->email; ?></td>
<td> <?php echo $row->answerA;?>
<?php echo $row->answerB;?>
<?php echo $row->answerC;?></td>
<td><?php echo $row->comment;?><br></td>
</tr>
<?php endforeach; ?>
</table>
<?php if (isset($pagination))
{
echo $pagination;
// echo "<pre>"; var_dump($query);
}
?>
答案 0 :(得分:1)
试试这个:
将此部件添加到控制器
$page = $this->uri->segment(3);
if($page == '')
{
$page = 1;
}
并修改此行
$data['records'] = $this->db->get('tblanswers, credentials', $config['per_page'], $page)->result_array();
编辑: 改变你的模特:
function result_getall()
{
$this->db->select('tblanswers.*,credentials.*');
$this->db->from('tblanswers');
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'LEFT');
$query = $this->db->get();
//return $query->result();
echo $this->db->last_query();
}
答案 1 :(得分:0)
控制器:
function getall()
{
$this->load->model('result_model');
$data['query'] = $this->result_model->result_getall();
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/Surva/index.php/result_controller/getall';
$config['total_rows'] = $this->db->get('tblanswers, credentials')->num_rows();
$config['per_page'] = 2;
$config['num_links'] = 5;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
/*
CHANGE $this->YOURMODELNAME-> to the name of the model
*/
$data['records'] = $this->YOUMODELNAME->result_getall($config['per_page'], $this->uri->segment(3));
$data['pagination'] = $this->pagination->create_links();
$this->load->view('result_view', $data);
}
型号:
function result_getall($perPage, $page)
{
$this->db->select('tblanswers.*,credentials.*');
$this->db->from('tblanswers');
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'LEFT');
$this->db->limit($perPage, $page);
$query = $this->db->get();
return $query->result();
}
我已将您需要修改的部分放在大写字母中。让我知道这是否成功
答案 2 :(得分:0)
在sql查询上使用限制功能。如果你想在sql查询限制0,10首次使用页面上显示10条记录