PHP code example of yidas / pagination

1. Go to this page and download the library: Download yidas/pagination library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yidas / pagination example snippets


// Get count of data set first
$sql = "SELECT count(*) FROM `table`"; 
$count = $conn->query($sql)->fetchColumn(); 

// Initialize a Data Pagination with previous count number
$pagination = new \yidas\data\Pagination([
    'totalCount' => $count,
]);

// Get range data for the current page
$sql = "SELECT * FROM `table` LIMIT {$pagination->offset}, {$pagination->limit}"; 
$sth = $conn->prepare($sql);
$sth->execute();
$data = $sth->fetchAll();

$query = $this->db->where('type', 'C');

// Clone same query for get total count
$countQuery = clone $query;

// Get total count from cloned query
// Or you could use count_all_results('', false) to keep query instead of using `clone`
$count = $countQuery->count_all_results();

// Initialize a Data Pagination with previous count number
$pagination = new \yidas\data\Pagination([
    'totalCount' => $count,
]);

// Get range data for the current page
$records = $query
    ->offset($pagination->offset)
    ->limit($pagination->limit)
    ->get()->result_array();

<div>
<?=\yidas\widgets\Pagination::widget([
    'pagination' => $pagination
])



$config['composer_autoload'] = TRUE;

// Get total rows from your query
$count = $query->count();
// Initialize a Data Pagination
$pagination = new \yidas\data\Pagination([
    'totalCount' => $count,
    'pergpage' => 10,
]);
// ...use $pagination offset/limit info for your query

namespace yidas\data;

use yidas\data\Pagination as BasePagination;

class Pagination extends BasePagination
{
    // Name of the parameter storing the current page index
    public $pageParam = 'page';
    
    // The number of items per page
    public $perPage = 10;
    
    // Name of the parameter storing the page size
    // false to turn off per-page input by client
    public $perPageParam = false;
}

/**
 * Yii 2 Framework sample code
 */
use yidas\data\Pagination;

// build a DB query to get all articles with status = 1
$query = Article::find()->where(['status' => 1]);

// get the total number of articles (but do not fetch the article data yet)
$count = $query->count();

// create a pagination object with the total count
$pagination = new Pagination(['totalCount' => $count]);

// limit the query using the pagination and retrieve the articles
$articles = $query->offset($pagination->offset)
    ->limit($pagination->limit)
    ->all();

use yidas\widgets\Pagination;

echo  Pagination::widget([
    'pagination' => $pagination
]);

echo  \yidas\widgets\Pagination::widget([
    'pagination' => $pagination,
    'view' => 'simple',
]);

echo  \yidas\widgets\Pagination::widget([
    'pagination' => $pagination,
    'view' => __DIR__ . '/../widgets/pagination_view.php',
]);



namespace app\widgets;

use yidas\widgets\Pagination as BaseWidget;

/**
 * Pagination Widget
 */
class Pagination extends BaseWidget
{
    // Set the Widget pager is center align or not   
    public $alignCenter = false;
    
    // Maximum number of page buttons that can be displayed   
    public $buttonCount = 7;

    // The text label for the "first" page button
    public $firstPageLabel = '<i class="fa fa-step-backward" aria-hidden="true"></i>';

    // The text label for the "last" page button
    public $lastPageLabel = '<i class="fa fa-step-forward" aria-hidden="true"></i>';
    
    // The text label for the "next" page button
    public $nextPageLabel = '<i class="fa fa-caret-right" aria-hidden="true"></i>';
    
    // The text label for the "previous" page button
    public $prevPageLabel = '<i class="fa fa-caret-left" aria-hidden="true"></i>';
    
    // <ul> class. For example, 'pagination-sm' for Bootstrap small size.
    public $ulCssClass = '';
}

// ex. https://yoursite.com/list/
// displays: https://yoursite.com/list/?page=100
echo $pagination->createUrl(100);

// ex. https://yoursite.com/list/?sort=desc&type=a
// displays: https://yoursite.com/list/?sort=desc&type=a&page=101
echo $pagination->createUrl(101);

// ex. https://yoursite.com/list/
// displays: https://yoursite.com/list/?page=1&per-page=50
echo $pagination->createUrl(1, 50);

$conn = new PDO("mysql:host=localhost;dbname=database", 'username', 'password');

// Get count of data set first
$sql = "SELECT count(*) FROM `table`"; 
$count = $conn->query($sql)->fetchColumn(); 

// Initialize a Data Pagination with previous count number
$pagination = new \yidas\data\Pagination([
    'totalCount' => $count,
]);

// Get range data for the current page
$sql = "SELECT * FROM `table` LIMIT {$pagination->offset}, {$pagination->limit}"; 
$sth = $conn->prepare($sql);
$sth->execute();
$data = $sth->fetchAll();

print_r($data);

echo yidas\widgets\Pagination::widget([
    'pagination' => $pagination
]);

$this->load->model('Post_model');

$query = $this->Post_model->find()
    ->where('type', 'C');
    
// Clone same query for get total count
$countQuery = clone $query;

// Get total count from cloned query
// Or you could use count(false) to keep query instead of using `clone`
$count = $countQuery->count();

// Initialize a Data Pagination with previous count number
$pagination = new \yidas\data\Pagination([
    'totalCount' => $count,
]);

// Get range data for the current page
$records = $query
    ->offset($pagination->offset)
    ->limit($pagination->limit)
    ->get()->result_array();

<div>
<?=yidas\widgets\Pagination::widget([
    'pagination' => $pagination
])