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;get_properties決して実行されません。そして、なぜそんなに多くのデータをセッションに保存しているのですか? これは必要ではなく、私の意見では良い考えではありません。

于 2010-08-27T13:33:27.167 に答える