Welcome Guest, Not a member yet? Register   Sign In
Can't get pagination working
#1

I'm developing a project with CI 3, everything works well. But I stuck in built-in pagination. Below are my controller, model, and view. I put comment inside just to see what was wrong in my code. Please note, I create ajax pagination in another controller within this project too and works well.

Any suggestion and help will be appreciated.


PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Page extends CI_Controller {

 public function 
__construct()
 {
 
parent::__construct();
 
$this->load->model('page_model');
 
$this->load->library('pagination');
 }

 public function 
index()
 {
 
$limit 10;

 
// This produce blank
 
$offset $this->uri->segment(30);


 
// Below code gives me error in sql query, i.e "You have an error in your SQL syntax; 
 // check the manual that corresponds to your MySQL server version for the right syntax to use near '-10' at line 1" --> LIMIT 10,-10. But this code works on my project using CI 2


 // $offset = ($this->uri->segment(3)-1) * $limit;


 // Produce 178 records
 
$data['totalrecords'] = $this->page_model->count_all()->num_rows();

 
$data['get_results'] = $this->page_model->get_search_result($limit$offset);


 
// When I changed variable $offset with a number, this work only in page 1.
 // But when I click page 2, 3, ..., no result displayed and it produces $totalrecords to be 0 in view
 // $data['get_results'] = $this->page_model->get_search_result($limit, 10);

 
$config = array();
 
$config['base_url'] = base_url().'page/index/';
 
       $config['total_rows'] = $data['totalrecords'];
 
       $config['per_page'] = $limit       
        $config
['uri_segment'] = 3;
 
       $config['use_page_numbers' TRUE;

 
       $this->pagination->initialize($config);
 
       $data['page_link'] = $this->pagination->create_links();

 
       $this->load->view('header');
 
       $this->load->view('search_result'$data);
 
       $this->load->view('footer');
 }




PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Page_model extends CI_Model {

 public function 
__construct()
 {
 
parent::__construct();
 
 }

 public function 
count_all() {
 
$key $this->input->post('s'TRUE);
 
$sql "SELECT p.*, c.* FROM posts as p JOIN categories as c ON c.category_id=p.post_id WHERE MATCH ('p.title, p.author, p.content, c.category_name') AGAINST ('.$key.')";
 
$query $this->db->query$sql );

 return 
$query;
 }

 public function 
get_search_result($limit$offset) {
 
$key $this->input->post('s'TRUE);
 
$sql "SELECT p.*, c.* FROM posts as p JOIN categories as c ON c.category_id=p.post_id WHERE MATCH ('p.title, p.author, p.content, c.category_name') AGAINST ('.$key.') LIMIT $limit,$offset";
 
$query $this->db->query$sql );

 return 
$query;
 }



Code:
<!-- Search Form -->
<div id="content">
<div class="container">
<h1>Search Form</h1>
<form action="<?php echo site_url('page/index'); ?>" method="post">
<input type="text" name="s" class="searchform" autocomplete="off">
<input type="submit" class="button-submit" value="Go Search">
</form>
</div>
</div>


Code:
<!-- Search Result -->
<div id="content">
<div class="container">
<h1>Total Records: <?php echo $totalrecords; ?></h1>
<hr>
<div id="result_container">
<ul>
<?php
foreach( $get_results->result() as $result ) :
$id = $result->id;
$author = $result->title;
?>
<li> <?php echo $id ."  - ". $author; ?> </li>
<?php endforeach; ?>
</ul>
<?php
// Actually the links work well as I see per uri->segment(3)
echo $page_link;
?>
</div>
</div>
</div>
Reply


Messages In This Thread
Can't get pagination working - by ditolakgebetan - 12-18-2015, 04:19 AM
RE: Can't get pagination working - by rymesaint - 12-20-2015, 04:50 PM
RE: Can't get pagination working - by skunkbad - 12-20-2015, 05:26 PM
RE: Can't get pagination working - by mike7418 - 12-27-2015, 04:18 PM



Theme © iAndrew 2016 - Forum software by © MyBB