Codeigniter分页问题

发布于 2024-09-16 06:13:41 字数 1294 浏览 5 评论 0原文

我基本上无法让分页位工作,我在更改数据库查询之前就这样做了,现在我陷入了困境。

我的模型看起来像:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情释 2024-09-23 06:13:41

它不起作用的原因是你永远无法获得total_rows。您通过此查询获得了total_rows,但它已经有一个偏移量和一个限制:

$sql .= " LIMIT ".$limit.", ".$offset.";";
$query = $this->db->query($sql);

要解决此问题,您应该向您的模型添加一个函数:

function get_all_properties()
{
    return $this->db->get('properties');
}

然后在您的控制器中,而不是:

$config['total_rows'] = $this->session->userdata('num_rows');

执行:

$config['total_rows'] = $this->properties_model->get_all_properties()->num_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:

$sql .= " LIMIT ".$limit.", ".$offset.";";
$query = $this->db->query($sql);

To fix this you should add a function to your model:

function get_all_properties()
{
    return $this->db->get('properties');
}

Then in your controller, instead of:

$config['total_rows'] = $this->session->userdata('num_rows');

Do:

$config['total_rows'] = $this->properties_model->get_all_properties()->num_rows();

This should fix your pagination. Other than this your code has some strange things. E.g. return FALSE; in get_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文