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(3, 0);
// 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>
-
rymesaint
Newbie
-
Posts: 6
Threads: 1
Joined: Nov 2015
Reputation:
0
same with me the pagination is't working..
-
skunkbad
Senior Citizen
-
Posts: 1,300
Threads: 63
Joined: Oct 2014
Reputation:
86
12-20-2015, 05:26 PM
(This post was last modified: 12-20-2015, 10:01 PM by skunkbad.)
First thing I see is that you are trying to implement paging on the index method of a controller, and this doesn't seem possible. Try changing to a different name for your method.
Next, in order to get pagination working easily, use a page parameter for the method, like this:
PHP Code: public function something( $page = 1 ){ // .... }
So you are now able to set the pagination base URL to "page/something", and your $page variable is easily used.
If you feel you must use the index method, you'll want to look at the "remapping" feature in the docs.
-
mike7418
Junior Member
-
Posts: 17
Threads: 3
Joined: Nov 2015
Reputation:
0
(12-20-2015, 05:26 PM)skunkbad Wrote: First thing I see is that you are trying to implement paging on the index method of a controller, and this doesn't seem possible. Try changing to a different name for your method.
Next, in order to get pagination working easily, use a page parameter for the method, like this:
PHP Code: public function something( $page = 1 ){ // .... }
So you are now able to set the pagination base URL to "page/something", and your $page variable is easily used.
If you feel you must use the index method, you'll want to look at the "remapping" feature in the docs.
Here is code that is working in 3.01 for me
PHP Code: if ($this->input->post('dept_id') != "") { $dept_id = $this->input->post('dept_id'); } else { $dept_id = $this->uri->segment(3,0); } $this->load->model('Db_model'); $page_cnt = 20; // Creating the pagination menu on the webpage $this->load->library('pagination'); $config['base_url'] = base_url("employee_list/display_result/$dept_id"); $config['total_rows'] = $this->Db_model->total_rows('employees', 'department_id', $dept_id); $config['per_page'] = $page_cnt; $config['attributes'] = array('class' => 'btn btn-default btn-lg active'); $config['cur_tag_open'] = "<b class='btn btn-primary btn-lg active'>"; $config['cur_tag_close'] = '</b>'; $this->pagination->initialize($config); $data['paginate'] = $this->pagination->create_links();
This is the SQL function I created for getting total rows
PHP Code: function total_rows($table ="", $field = "", $id = "") { $result = ""; if ($field != "" && $id != "") { $this->db->where($field, $id); $this->db->from($table); $result = $this->db->count_all_results(); } if ($result != "") { return $result; } else { return array('error'=>'No result found'); } }
|