Codeigniter分页问题
我基本上无法让分页位工作,我在更改数据库查询之前就这样做了,现在我陷入了困境。
我的模型看起来像:
function get_properties($limit, $offset) {
$location = $this->session->userdata('location');
$property_type = $this->session->userdata('property_type');
if($property_type == 0)
{
$sql = "SELECT * FROM properties ";
}
// more queries here
$sql .= " LIMIT ".$limit.", ".$offset.";";
$query = $this->db->query($sql);
if($query->num_rows() > 0) {
$this->session->set_userdata('num_rows', $query->num_rows());
return $query->result_array();
return FALSE;
}
}
}
我的控制器看起来像:
function results() {
$config['base_url'] = base_url().'/properties/results';
$config['per_page'] = '3';
$data['properties_results'] = $this->properties_model->get_properties($config['per_page'], $this->uri->segment(3));
$config['total_rows'] = $this->session->userdata('num_rows');
$this->pagination->initialize($config);
$config['full_tag_open']='<div id="pages">';
$config['full_tag_close']='</div>';
$data['links']=$this->pagination->create_links();
$this->load->view('properties_results',$data);
}
请帮忙......它搞砸了!
I basically can’t get the pagination bit to work, I did before I changed my database query and now I’m stuck.
My model looks like:
function get_properties($limit, $offset) {
$location = $this->session->userdata('location');
$property_type = $this->session->userdata('property_type');
if($property_type == 0)
{
$sql = "SELECT * FROM properties ";
}
// more queries here
$sql .= " LIMIT ".$limit.", ".$offset.";";
$query = $this->db->query($sql);
if($query->num_rows() > 0) {
$this->session->set_userdata('num_rows', $query->num_rows());
return $query->result_array();
return FALSE;
}
}
}
and my controller looks like:
function results() {
$config['base_url'] = base_url().'/properties/results';
$config['per_page'] = '3';
$data['properties_results'] = $this->properties_model->get_properties($config['per_page'], $this->uri->segment(3));
$config['total_rows'] = $this->session->userdata('num_rows');
$this->pagination->initialize($config);
$config['full_tag_open']='<div id="pages">';
$config['full_tag_close']='</div>';
$data['links']=$this->pagination->create_links();
$this->load->view('properties_results',$data);
}
please help…its screwing up!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不起作用的原因是你永远无法获得total_rows。您通过此查询获得了total_rows,但它已经有一个偏移量和一个限制:
要解决此问题,您应该向您的模型添加一个函数:
然后在您的控制器中,而不是:
执行:
这应该修复您的分页。除此之外,你的代码还有一些奇怪的事情。例如,
get_properties
中的return FALSE;
将永远不会执行。为什么要在会话中存储这么多数据。在我看来,这是没有必要的,也不是一个好主意。The reason it's not working is that you never get the total_rows. You get the total_rows via this query, but it already has an offset and a limit:
To fix this you should add a function to your model:
Then in your controller, instead of:
Do:
This should fix your pagination. Other than this your code has some strange things. E.g.
return FALSE;
inget_properties
will NEVER execute. And why are you storing so much data in sessions. This is not necessary and not a good idea in my opinion.