Welcome Guest, Not a member yet? Register   Sign In
How to make paging from the basics
#1

Hi guys, I'm sorry to open a new topic, but I'm learning with Codeigniter, I read everywhere guides and help on the internet but did not find anything that really explained from scratch how to structure the pagination.

I've tried a lot of things and also tried here on the forum, doing so many tests, but nothing, just showing me the pagination but not working.

This is my controller about database results:

PHP Code:
class Post extends CI_Controller {
 
   
    public 
function __construct() {
 
       parent::__construct();
 
       $this->load->model('post_model');
 
       $this->load->helper('url_helper');
 
       $this->load->helper('text');
 
   }
 
   
    public 
function index() {
 
       $data['title'] = 'Site';
 
       $data['desc'] = 'Enter the description of this page index';
 
       $data['posts'] = $this->post_model->get_post();
 
       $this->load->view('templates/header'$data);
 
       $this->load->view('index'$data);
 
     


While this is the model, in a few words are exactly the code provided by the codeigniter user guide, about tutorials to create news.


PHP Code:
class Post_model extends CI_Model {
 
   
    public 
function __construct() 
 
   {
 
       $this->load->database();
 
   }
 
   
    public 
function get_post($slug FALSE
 
   {
 
       if ($slug === FALSE) {
 
           $this->db->select('id,slug,text,gender,age,views,time,status');
 
           $this->db->from('post');
 
           $this->db->where('status'1);
 
           $this->db->order_by('id''DESC');
 
           $query $this->db->get();
 
           return $query->result_array();
 
       }
 
       $this->db->set('views''views+1'FALSE);
 
       $this->db->where('slug'$slug);
 
       $this->db->update('post');
 
       $query $this->db->get_where('post', array('slug'=>$slug));
 
       return $query->row_array();
 
   


I have read this guide https://www.codeigniter.com/user_guide/l...on#example  but I can not personalize everything and then run it. Do I need to create a specific file controller? I do not know how to move.

Thanks to who will help me,
Reply
#2

1. I put default pagination configs in application/config/pagination.php :
PHP Code:
<?php if (!defined('BASEPATH'))
exit(
'No direct script access allowed');

$config['per_page'] = 10;
$config['num_links'] = 3;
$config['page_query_string'] = TRUE;
$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] = "</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";
$config['last_link'] = 'first';
$config['first_link'] = 'last'

2. In controller 

PHP Code:
class Post extends CI_Controller{

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

    public function all_posts() 
    
{
        $this->load->library('pagination');

        $offset 0;
        
//per_page is the variable of pagination library, if you want you can change this variable name in pagination configs
        if ($this->input->get('per_page') != null) {
            $offset $this->input->get('per_page');
        }
        $count 10//number of posts in page 
       
filter = array();
        $data['post_count'] = $this->post_model->get_total_post_count($filter); //pagination need to know how many total posts you have 

        $data['posts'] = $this->post_model->get_posts($filter$limit$offset);
        
        $config
['base_url'] = site_url() . 'post/all_posts';
        $config['total_rows'] = $data['post_count'];
        $config['per_page'] = $count;

        $this->pagination->initialize($config);

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

        $this->load->view('templates/header'$data);
    
    


in view: 
PHP Code:
<?php echo $pagination//to show pagination links; ?>
Reply
#3

Hi and thank you for your help, unfortunately I found some mistakes in adapting my code.

Is there some way to adapt it to my controller public function index() ?

PHP Code:
class Post extends CI_Controller {
 
   
    public 
function __construct() {
 
       parent::__construct();
 
       $this->load->model('post_model');
 
       $this->load->helper('url_helper');
 
       $this->load->helper('text');
 
   }
 
   
    public 
function index() {
 
       $data['title'] = 'Title';
 
       $data['desc'] = 'Enter the description of this page index';
 
       $data['post'] = $this->post_model->get_post();    
        $this
->load->view('templates/header'$data);
 
       $this->load->view('index'$data);
 
     
Reply
#4

first imlement get_posts() and get_post_count() methods in your post_model,

get_posts($filter, $offset, $limit) will filter posts according to data in $filter, and get posts starting from $offset and ending with  $limit;
get_post_count($filter) will return total post count
Reply
#5

(This post was last modified: 08-10-2017, 03:57 AM by Marcolino92.)

PHP Code:
A PHP Error was encountered

Severity
Notice

Message
Undefined variablelimit 

I have inserted the limit variable in the controller, not any error on the page.

The only thing is that the url of the site does not work, so it does not return the page:

http://www.mysite.it/post/hot?per_page=10
Reply
#6

PHP Code:
class Post extends CI_Controller{

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

 
   public function all_posts() 
 
   {
 
       $this->load->library('pagination');

 
       $offset 0;
 
       //per_page is the variable of pagination library, if you want you can change this variable name in pagination configs
 
       if ($this->input->get('per_page') != null) {
 
           $offset $this->input->get('per_page');
 
       }
 
       $count 10//number of posts in page 
 
      filter = array();
 
       $data['post_count'] = $this->post_model->get_total_post_count($filter); //pagination need to know how many total posts you have 

 
       $data['posts'] = $this->post_model->get_posts($filter$count $offset);
 
       
        $config
['base_url'] = site_url() . 'post/all_posts';
 
       $config['total_rows'] = $data['post_count'];
 
       $config['per_page'] = $count;

 
       $this->pagination->initialize($config);

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

 
       $this->load->view('templates/header'$data);
 
   
    

Reply
#7

in your controller method: 
$config['base_url'] = site_url() . 'post/all_posts'; //this line sets page url, if your page url is different update it 
Reply
#8

Ops, I've noticed that all the results are shown, even though I set the maximum number per page.

I suppose you have to work in the method, and set limits, offsets?
How can I do?
Reply
#9

$this->input->get('per_page'); this is your offset you can use its value in your db query 
value of $limit is up to you
Reply
#10

controller

PHP Code:
   public function index() {
 
       $this->load->library('pagination');
 
       $offset 0;
 
       //per_page is the variable of pagination library, if you want you can change this variable name in pagination configs
 
       if ($this->input->get('per_page') != null) {
 
           $offset $this->input->get('per_page');
 
       }
 
       $count 5//number of posts in page 
 
       $filter = array();
 
       
        $data
['post_count'] = $this->post_model->get_total_post($filter);
 
       
        $data
['title'] = 'title';
 
       $data['desc'] = 'Enter the description of this page hot';
 
       $data['post'] = $this->post_model->get_hotPost($filter$count$offset);
 
       
        $config
['base_url'] = base_url() . 'hot';
 
       $config['total_rows'] = $data['post_count'];
 
       $config['per_page'] = $count;

 
       $this->pagination->initialize($config);
 
       $data['pagination'] = $this->pagination->create_links();
 
       
        $this
->load->view('templates/header'$data);
 
       $this->load->view('post/hot'$data);
 
   

Model

PHP Code:
   public function get_hotPost($filter$count$offset
 
   {
 
       $this->db->select('id,slug,text,gender,age,views,time,status');
 
       $this->db->from('post');
 
       $this->db->where('status'1);
 
       $this->db->order_by('views''DESC');
 
       $this->db->limit($count);
 
       $query $this->db->get();
 
       return $query->result_array();
 
   

Unfortunately I'm shown a lot of results (as I have set) and going to the next page shows me the same results. I do not understand why
Reply




Theme © iAndrew 2016 - Forum software by © MyBB