Skip to content

Instantly share code, notes, and snippets.

@davidkevork
Last active August 29, 2015 14:25
Show Gist options
  • Save davidkevork/61a2f862465d2f6885ae to your computer and use it in GitHub Desktop.
Save davidkevork/61a2f862465d2f6885ae to your computer and use it in GitHub Desktop.
<?php
include './congif.php';
/**
* config class should have __construct() and __destruct() mysqli connect and close functions
*/
class pagination extends config
{
public $PerPage = '10';
public $PerPagination = '10';
public $FistPage = 1;
public $LastPage;
public $DefaultPage = 'pagination.php';
public $html;
public $pagination;
public function __construct()
{
return parent::__construct();
}
public static function CurrentPage()
{
if (isset($_GET['page']) && !empty($_GET['page'])) {
if (filter_var((int)$_GET['page'], FILTER_VALIDATE_INT)) {
if ($_GET['page'] > 0) {
return ((int) $_GET['page']);
} else {
return ((int) 1);
}
} else {
return ((int) 1);
}
} else {
return ((int) 1);
}
}
public static function NextPage()
{
$self = new pagination;
if (self::CurrentPage() < $self->TotalPages()) {
return (self::CurrentPage() + 1);
} else {
return self::CurrentPage();
}
}
public static function PreviousPage()
{
if (self::CurrentPage() > 1) {
return (self::CurrentPage() - 1);
} else {
return self::CurrentPage();
}
}
public function ShowUsers()
{
// edit is as you want but don't forget to use ORDER BY `id` DESC LIMIT ?, ? at the end of the sql
// so it will give you the exact data from the database
$this->CurrentPage = self::CurrentPage();
$this->sql = "SELECT `id`, `username`, `password` FROM `user` ORDER BY `id` DESC LIMIT ?, ?";
$this->MinId = ($this->CurrentPage * $this->PerPage) - $this->PerPage;
$this->AllData = $this->mysqli->prepare($this->sql);
$this->AllData->bind_param('ii', $this->MinId, $this->PerPage);
$this->AllData->execute();
$this->AllData->store_result();
if ($this->AllData->num_rows > 0) {
$this->AllData->bind_result($this->id, $this->username, $this->password);
while ($this->AllData->fetch()) {
$this->html .= $this->id.' '.$this->username.' '.$this->password;
}
$this->AllData->close();
return $this->html;
}
$this->AllData->close();
}
public function ShowPaginationBootstrap()
{
// there is some bug here but i don't know what is it if you found it
// tell us so we fix the bug as soon as possible
$self = new pagination;
$this->CurrentPage = self::CurrentPage();
if (self::TotalPages() > 0) {
if ($this->CurrentPage > self::TotalPages()) {
header('Location: '.$this->DefaultPage.'?page='.self::TotalPages());
}
$this->pagination = '<nav>';
$this->pagination .= '<ul class="pagination">';
if ($this->CurrentPage != self::PreviousPage()) {
$this->pagination .= '<li><a class="pagination-css" data-toggle="tooltip" href="'.$this->DefaultPage.'?page='.self::PreviousPage().'" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';
} else {
$this->pagination .= '<li class="disabled"><a class="pagination-css" data-toggle="tooltip" href="'.$this->DefaultPage.'?page='.self::PreviousPage().'" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';
}
$this->TotalShows = 1; // max is $this->PerPagination;
$this->LastPage = self::TotalPages();
$this->Decreased = 0;
if ($self->TotalPages() > 0) {
$this->Page = $this->CurrentPage;
while ($this->Page > $this->PerPagination) {
$this->Page -= $this->PerPagination;
$this->Decreased++;
}
while ($this->TotalShows <= $this->PerPagination && ($this->TotalShows + ($this->PerPagination * $this->Decreased)) <= $this->TotalPages) {
if (($this->TotalShows + ($this->PerPagination * $this->Decreased)) == $this->CurrentPage) {
$this->pagination .= '<li class="active"><a class="pagination-css" data-toggle="tooltip" href="'.$this->DefaultPage.'?page='.($this->TotalShows + ($this->PerPagination * $this->Decreased)).'">'.($this->TotalShows + ($this->PerPagination * $this->Decreased)).'</li>';
} else {
$this->pagination .= '<li><a class="pagination-css" data-toggle="tooltip" href="'.$this->DefaultPage.'?page='.($this->TotalShows + ($this->PerPagination * $this->Decreased)).'">'.($this->TotalShows + ($this->PerPagination * $this->Decreased)).'</li>';
}
$this->TotalShows++;
}
}
if ($this->CurrentPage != self::NextPage()) {
$this->pagination .= '<li><a class="pagination-css" data-toggle="tooltip" href="'.$this->DefaultPage.'?page='.self::NextPage().'" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>';
} else {
$this->pagination .= '<li class="disabled"><a href="'.$this->DefaultPage.'?page='.self::NextPage().'" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>';
}
$this->pagination .= '</ul>';
$this->pagination .= '</nav>';
return $this->pagination;
}
}
public function TotalPages()
{
$this->sql = "SELECT * FROM `user`";
$this->TakePages = $this->mysqli->prepare($this->sql);
$this->TakePages->execute();
$this->TakePages->store_result();
$this->TotalPages = ceil($this->TakePages->num_rows / $this->PerPage);
$this->TakePages->close();
return $this->TotalPages;
}
public function __destruct()
{
return parent::__destruct();
}
}
?>
@davidokedion
Copy link

Where is the config file you are extending. You didn't upload it.

@davidkevork
Copy link
Author

mysqli = new mysqli($this->hostname, $this->username, $this->password, $this->dbName); if ($this->mysqli->connect_errno) { return '
  • Error Connecting to the Database
'; } else { return '
  • Connected to the Database
'; } } public function __destruct() { return $this->mysqli->close(); } ``` } ?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment