Coder Social home page Coder Social logo

paginator's Introduction

paginator

A Codeigniter library to add pagination easily

Simple Usage:

Download and copy the Paginator.php file to your Codeigniter libraries folder

In your controller use it like the following :

$this->load->library('paginator'); // load paginator library
$data['posts'] = $this->paginator->paginate('posts'); // per page equal 10 by default

You can customize 3 optional options (per_page, query_string (true/false), base_url) by passing them as an array for the second parameter:

$data['posts'] = $this->paginator->paginate('posts', ['per_page' => 15, 'query_string' => true, 'base_url' => 'posts']);

you can read more about Codeigniter pagination options here : https://www.codeigniter.com/userguide3/libraries/pagination.html

Finally in your view to add the pagination links :

<?php echo $this->paginator->get_links('posts', 'bootstrap4'); ?>

The second parameter is optional, the available options currently are: 'bootstrap3' or 'bootstrap4' or 'bulma'.

Customize the Result:

Paginator allows you to specify where, like and order_by options to customize your query result like the following:

$data['posts'] = $this->paginator->paginate('posts', ['base_url' => "posts", 'where' => array('published' => '1'), 'order_by' => 'id asc' , 'per_page' => 15]);

or with like :

$data['posts'] = $this->paginator->paginate('posts', ['base_url' => "posts", 'like' => array('title' => 'some term here'), 'order_by' => 'id asc' , 'per_page' => 15]);

And that's fine for most situations, but what if you need more complex request ?

In this case :

1- get rid of where, like and order_by options

2- replace the very first parameter of paginate() method with custom query builder result, for example :

$this->load->model('YourModel');
$custom_object = $this->YourModel->get_custom_db_obj();
$data['posts'] = $this->paginator->paginate($custom_object, ['base_url' => "posts", 'per_page' => 15]);

In your model you might have a method that returns the db object, for example

function get_custom_db_obj()
{
  # you can have any complex query here:
  $this->db->select('*');
  $this->db->from('posts');
  $this->db->where(array('published'=>'1'));
  $this->db->order_by('created_at', 'DESC');
  return $this->db; // make sure to return $this->db without calling ->get() on it
}

It's very important to note that you should never call or return $this->db->get() but only return $this->db, also avoid using limit() on your request building because it's the role of paginator to use it internally

Feedback

Please use the Issues for any bugs, feature requests, etc.

License

MIT License

paginator's People

Contributors

scratchoo avatar

Stargazers

 avatar Invalid Name avatar George Tsachrelias avatar abel-k avatar

Watchers

James Cloos avatar  avatar abel-k avatar Invalid Name avatar

Forkers

ajotah brun025

paginator's Issues

Link last page

Hello, I have detected that after the last update the link to go to the last page does not work, is it possible that it is like this?

Validation issue

Hi There!
Library works great but I found a couple of troubles related to the query_string value.

If you send a number over the limit like for example (?page=99) your library show no message or have a routine to send it to the first page.
Also if you put manually a string like for example (?page=x) will show you an error because the query can't be executed.

Cheers!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.