1

我基本上无法让分页位工作,我在更改数据库查询之前做过,现在我被卡住了。

我的模型看起来像:

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);
} 

请帮助......它搞砸了!

4

1 回答 1

1

它不起作用的原因是你永远不会得到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();

这应该可以解决您的分页问题。除此之外,您的代码还有一些奇怪的东西。例如return FALSE;inget_properties将永远不会执行。为什么要在会话中存储这么多数据。在我看来,这不是必要的,也不是一个好主意。

于 2010-08-27T13:33:27.167 回答