Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

My paging class

Featured Replies

Heres a simple class i made for doing pagination

 

<?php
class paginate{
  /**Our private variables**/
  private $_query = '';
  private $_count = '';
  private $_pages = '';
  private $_current = '';
  private $page = '';
  private $limit = 2;
  private $next = '';
  private $prev = '';
  private $link = '';
   /**Our public variables**/
  public $limit_start = '';
  public $limit_end = '';

  public function __construct(){
    $this->page = (!$_GET['page'] || $_GET['page'] < 0) ? "1" : $_GET['page'];
    $this->page = ceil($this->page);
    $this->limit_start = $this->limit;
    $this->limit_end = $this->page*$this->limit-($this->limit);
  }
  public function paginate_me($current_page,$query){
  $this->_query = mysql_query($query);
  $this->count = mysql_num_rows($this->_query);
  $this->_pages = ceil($this->count/$this->limit);
  $this->_current = $current_page;
  $this->next = ($this->page+1 > $this->_pages) ? "Next" : "<a href='".$this->_current.".php?page=".($this->page+1)."'>Next</a>";
  $this->prev = ($this->page-1 <= 0) ? "Prev" : "<a href='".$this->_current.".php?page=".($this->page-1)."'>Prev</a>";
  /**Echo out the prev link**/
  echo $this->prev;
  for($i = 1; $i<=$this->_pages;$i++){
    $this->link = ($this->page == $i) ? " ".$i." " : "<a href='".$this->_current.".php?page=".$i."'>$i</a>";
    echo $this->link;
  }
  echo $this->next;
  }
}


?>

 

To use it just connect with mysql and then call it like this

 

$page = new paginate;
$page->paginate_me("test","SELECT * FROM test");

 

where it says test thats the name of the page your gonna paginate, then the other param is the query that is used to get the row count ect.. of a certain table. Lastly u need to set the limit within whatever query your using to pull your results

 

$query = mysql_query("SELECT * FROM test ORDER BY id DESC LIMIT ".$page->limit_end.",".$page->limit_start."");

Edited by webdesigner93

  • Author

I personally tend to use jay'spagination class quite a lot :)

 

Although I always liked pear pager class too as it can generate links for larger data sets as follows (don't know the name of it):

 

45 46 47 . . . . 59 60 61 etc.

Yea i took a look at his, looks pretty good :) have personally not used it i prefer making my own but it deff looks like he put quite abit of extras in it :)

  • Author

I personally tend to use jay'spagination class quite a lot :)

 

Although I always liked pear pager class too as it can generate links for larger data sets as follows (don't know the name of the following style):

 

45 46 47 . . . . 59 60 61 etc.

 

For your's, the first thing that stands out to me are the links it produces, they are hard coded in. Being able to set a more seo friendly type of URL would help E.g.

 

site.com/articles/page/20 etc

 

You are also forcing people the use the old mysql (any reasoin you're not using mysqli????).

 

Also, you need some filtering of your get variables.

 

Not knocking, just pointing stuff out.

 

Im actually gonna throw in a param for them to include there $mysqli link then switch it over to mysqli and thank you for pointing out the filtering thing i actually over looked it by accident :)

Edited by webdesigner93

  • Author

Ok i switched all of this to use php 5's mysqli insted of mysql,

 

<?php
class paginate{
  /**Our private variables**/
  private $_query = '';
  private $_count = '';
  private $_pages = '';
  private $_current = '';
  private $page = '';
  private $next = '';
  private $prev = '';
  private $link = '';
  private $connection = false;
   /**Our public variables**/
  public $limit = '';
  public $limit_start = '';
  public $style_class = '';
  public $limit_end = '';

  public function __construct($connect){
    /**Set up our variables that needs to be called at the start of this script**/
    $this->page = (!$_GET['page'] || $_GET['page'] < 0) ? "1" : $_GET['page'];
    $this->page = ceil($this->page);
    $this->limit_start = $this->limit;
    $this->limit_end = $this->page*$this->limit-($this->limit);
    $this->connection = $connect;
  }
  public function paginate_me($current_page,$query){

  $this->_query = @mysqli_query($this->connection,$query);
  if(!$this->_query){
    die(mysqli_error($conn));
  }
  $this->count = mysqli_num_rows($this->_query);
  $this->_pages = ceil($this->count/$this->limit);

  $this->_current = $current_page;
  $this->next = ($this->page+1 > $this->_pages) ? "Next" : "<a href='".$this->_current.".php?page=".($this->page+1)."'>Next</a>";
  $this->prev = ($this->page-1 <= 0) ? "Prev" : "<a href='".$this->_current.".php?page=".($this->page-1)."'>Prev</a>";
  /**Echo out the prev link**/
  echo "<div class='".$this->style_class."'>";
  echo $this->prev;
  for($i = 1; $i<=$this->_pages;$i++){
    $this->link = ($this->page == $i) ? " ".$i." " : "<a href='".$this->_current.".php?page=".$i."'>$i</a>";
    echo $this->link;
  }
  echo $this->next;
  echo "</div>";
  }
}


?>

 

to set up the object do

 

$page = new paginate($mysqli);//Include mysqli connection link

 

then set up some options

 

$page->limit = 20;/**How many results you want on each page**/
$page->style_class = 'myclass';/**Name of class you wanna use to style the links**/

 

Then call the paginate me method

 

$page->paginate_me("search","SELECT * FROM table");/**Page name, and then query you wanna get number of results from**/  

/**Lastly use the $page->limit_end,and $page->limit_start in your query that pulls the results out of mysql**/
$myquery = mysqli_query($conn,"SELECT * FROM table ORDER BY id DESC LIMIT ".$page->limit_end.",".$page->limit_start."");

 

Thats all you have to do inorder to paginate your results if you have questions please message me :)

Edited by webdesigner93

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.