Click here to Skip to main content
15,881,866 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My problem is that I want to use pagination for different pages but they are in one controller. The pagination for get_all_2017 is successful but the get_all_2018 does not function properly because when I click it only retrieve the first 100 values.


What I have tried:

This is my Controller

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Product extends CI_Controller
{

    public function _Construct()
    { 
        parent::__Construct();
        cek_login();
    }

    public function index($offset=0)
    {

        $this->load->library('pagination');
        $config['base_url'] = site_url('product/index');
        $config['total_rows'] = $this->M_product->countAll();
        $config['per_page'] = 100;

        $config['full_tag_open'] = '<ul class="pagination">';        
        $config['full_tag_close'] = '</ul>';        
        $config['first_link'] = 'First';        
        $config['last_link'] = 'Last';        
        $config['first_tag_open'] = '<li class="page-item">';        
        $config['first_tag_close'] = '</li>';        
        $config['prev_link'] = '«';        
        $config['prev_tag_open'] = '<li class="page-item">';        
        $config['prev_tag_close'] = '</li>';        
        $config['next_link'] = '»';        
        $config['next_tag_open'] = '<li class="page-item">';        
        $config['next_tag_close'] = '</li>';        
        $config['last_tag_open'] = '<li class="page-item">';        
        $config['last_tag_close'] = '</li>';        
        $config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';        
        $config['cur_tag_close'] = '</a></li>';        
        $config['num_tag_open'] = '<li class="page-item">';        
        $config['num_tag_close'] = '</li>';

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

        $data ['product17'] = $this->M_product->get_all_2017($config['per_page'],$offset);

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

        $this->template->load('back/template','back/product/data_product', $data);
    }

    public function get_product18($offset=0)
    {

        $this->load->library('pagination');
        $config['base_url'] = site_url('product/get_product18/index');
        $config['total_rows'] = $this->M_2018->countAll();
        $config['per_page'] = 100;

        $config['full_tag_open'] = '<ul class="pagination">';        
        $config['full_tag_close'] = '</ul>';        
        $config['first_link'] = 'First';        
        $config['last_link'] = 'Last';        
        $config['first_tag_open'] = '<li class="page-item">';        
        $config['first_tag_close'] = '</li>';        
        $config['prev_link'] = '«';        
        $config['prev_tag_open'] = '<li class="page-item">';        
        $config['prev_tag_close'] = '</li>';        
        $config['next_link'] = '»';        
        $config['next_tag_open'] = '<li class="page-item">';        
        $config['next_tag_close'] = '</li>';        
        $config['last_tag_open'] = '<li class="page-item">';        
        $config['last_tag_close'] = '</li>';        
        $config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';        
        $config['cur_tag_close'] = '</a></li>';        
        $config['num_tag_open'] = '<li class="page-item">';        
        $config['num_tag_close'] = '</li>';

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

        $data ['product18'] = $this->M_2018->get_all_2018($config['per_page'],$offset);

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

        $this->template->load('back/template','back/product/data_product18', $data);
    }



This is my model

<?php
defined('BASEPATH') or exit ('No direct script access allowed');

/**
 * 
 */
class M_product extends CI_Model
{
	public function get_all_2017($limit,$offset)
	{
		$this->db->limit($limit);
		$this->db->offset($offset);


		return $this->db->get('all_2017')->result();
	}

	function countAll()
	{
		return $this->db->get('all_2017')->num_rows();
        return $this->db->get('all_2018')->num_rows();
		
		
	}

	public function get_all_2018()
	{
	
        $this->db->limit($limit);
		$this->db->offset($offset);

		return $this->db->get('all_2018')->result();
	}


This is my view for both

/table> <br>
                <?php echo $pagination; ?>
                    </div>
Posted
Updated 30-May-22 21:17pm

1 solution

Hello !

your var $config['per_page'] seems to invoke a max limit for display of items.
change it for more => 1000 ??? 100000 ???
 
Share this answer
 
Comments
Nct Zen 31-May-22 3:23am    
$config['per_page] = 100 means that at first page it contain 100 value after that in second page it generate another 100. So if I have 200 data first 100 on page 1 and another on the second page. But for now, the second page has the same value as page number 1 it is supposed to generate new data not the same as the first page. (sorry for my bad english)
Member 15627495 31-May-22 4:16am    
ok , the value 100 is relevant then.

maybe a tail in your query is missing , you know the "limit" statement in sql.
it indicates a range for the results.

limit 100 offset 5
limit 1000

you have to query again for each new page on display.

Understanding better, the first page you display have 100 or less results,
but the second page have more than 100 result and you want to reach the result in the range 100 to 200 / 200 to 300 / 300 to 400 ( by range width of 100 )

https://www.w3schools.com/sql/sql_top.asp // limit use in this page
https://stackoverflow.com/questions/14536013/what-is-the-difference-between-mysql-limit-range-of-0-500-and-1-500 // limit and between for range fetch



as $offset is 0 by default in input of function ($offset=0) ,
you have to put a new value when calling

at public function index(new value needed)

because it make starting the index at 0 by default, so every time you call this function the range is 0 to 100
Nct Zen 31-May-22 4:40am    
Okay thankyou i'll try :)
Member 15627495 31-May-22 8:22am    
in php functions,
when a function have in its declaration an input : function TheFunction($var = 'default_value'){ echo($var);use($var).... }
the fire of TheFunction() make the value of $var equal to 'default_value'

if you want something different, you have to write it.
TheFunction('another_value'); // it will display 'another_value' , instead of using the 'default_value'

that is the difference between :
function one($input){......} //
function two($input = 'something'){ ..... } in this case, if $input is not written, it will be equal to 'something'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900