Simple Pagination in Php

23-09-2017

Php Codes

<div class="pagination-wrapper">
<ul class="pagination">

<li><a href="?page=<?= $page > 1 ? ( $page - 1 ) : '1' ?>" aria-label="Previous"><span
aria-hidden="true"><i
class="fa fa-long-arrow-left"></i> Önceki Sayfa</span></a></li>
<?php
$databaseUtil = new \hatim\Libraries\DatabaseUtil();
$limit        = 2;
$total        = $databaseUtil->count( "hatim" );
$pages        = ceil( $total / $limit );
// What page are we currently on?
$page = get_query_var( "page" );
$page = $page == 0 ? 1 : $page;

// Calculate the offset for the query
$offset = ( $page - 1 ) * $limit;

$hatims = $databaseUtil->all( 'hatim', $limit, $offset );
$link = "";
if ( $pages >= 1 && $page <= $pages ) {
    $counter = 1;
    $link    = "";
    if ( $page > ( $limit / 2 ) ) {
        $link .= "<li><a href=\"?page=1\">1 </a> </li>... ";
    }
    for ( $x = $page; $x <= $pages; $x ++ ) {
        
        if ( $counter <= $limit ) {
            if ( $page == $x ) {
                $link .= "<li class='active'><a href=\"?page=" . $x . "\">" . $x . " </a></li>";
            } else {
                $link .= "<li><a href=\"?page=" . $x . "\">" . $x . " </a></li>";
            }
        }
        
        $counter ++;
    }
    if ( $page < $pages - ( $limit / 2 ) ) {
        $link .= "... " . "<li><a href=\"?page=" . $pages . "\">" . $pages . " </a></li>";
    }
}
echo $link; ?>
<li><a href="?page=<?= $page + 1 ?>" aria-label="Next">
<span aria-hidden="true">Sonraki Sayfa <i class="fa fa-long-arrow-right"></i></span>
</a>
</li>

</ul>
</div>

DatabaseUtil Class

class DatabaseUtil{
    public function count( $table ) {
        global $wpdb;
        $table_name = $wpdb->prefix . $table;
        return intval($wpdb->get_row( "select count(*) as count from $table_name" )->count);
    }

    public function all( $table, $limit, $offset = 0 ) {
        global $wpdb;
        $table_name = $wpdb->prefix . $table;
        
        return $wpdb->get_results( "select * from " . $table_name .
        " order by id desc".
        "  LIMIT " . $limit .
        " OFFSET " . $offset  );
    }
}

Styling

/* Pagination */

.pagination-wrapper {

}

.pagination {
    font-size: 14px;

    color: #fff;
}

.pagination-wrapper ul {
    list-style: none;
}

.pagination li {
    float: left;
    text-decoration: none;
}

.pagination li:first-child a,
.pagination li:last-child a {
    padding: 12px 30px;
}

.pagination li:nth-child(11) a {
    padding-right: 92px;
}

.pagination li a {
    color: #fff;
    background-color: #27292a;
    border: 1px solid #0e0f0f;
    padding: 12px 15px;
    text-decoration: none;
}

.pagination li a:hover,
.pagination .active > a,
.pagination .active > a:focus,
.pagination .active > a:hover {
    color: #fff;
}

.page-breadcrumbs {
    position: relative;
    margin: 85px 0 15px;
}
.pagination > li:last-child > a, .pagination > li:last-child > span {
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
}

.top-user-section a:hover,
.post-content .entry-meta li:hover i,
.list-post li a:hover i,
.gallery-turner a:hover,
.widget .nav-justified > li > a:hover i,
.widget .nav-tabs.nav-justified > .active > a i,
.widget .tab-content .entry-meta li a:hover,
.footer-menu .navbar-nav li a:hover,
.widget .tab-pane .tag-cloud li a:hover,
#twitter-feed a:hover,
.widget address a:hover,
.details-news .entry-meta .posted-by a:hover,
.comments-wrapper .media-body h2 a:hover,
.comments-wrapper .media-body .replay,
.author-listing .single-author h2 a:hover,
.single-member .social-icons li i,
.contact-info ul li a:hover,
.subscribe-me h1 {
    color: #ed1c24;
}

.section-title:before,
.btn-primary,
.badge,
.catagory a,
.owl-theme .owl-controls .owl-page.active span,
.owl-theme .owl-controls.clickable .owl-page:hover span,
#latest-news .owl-controls .owl-page.active span,
#latest-news .owl-controls.clickable .owl-page:hover span,
.widget.news-letter button,
.pagination li a:hover,
.pagination .active > a,
.pagination .active > a:focus,
.pagination .active > a:hover,
#breaking-news span,
.subscribe-me button,
.top-user-form .btn:hover {
    background-color: #ed1c24;
}

.form-control:focus,
.widget.news-letter input:focus,
.pagination li a:hover,
.pagination .active > a,
.pagination .active > a:focus,
.pagination .active > a:hover,
.subscribe-me button,
.btn-danger {
    border-color: #ed1c24;
}

#mainmenu .sub-catagory ul li a:hover,
.cat-menu .active a,
.cat-menu a:hover {
    border-bottom: 2px solid #ed1c24;
}

.subscribe-me button:hover,
.top-user-form .btn,
.widget.news-letter button:hover,
.widget.news-letter button:focus {
    background-color: #f14950
}

.single-member .overlay {
    background-color: rgba(237, 28, 36, 0.75);
}

Result

© 2019 All rights reserved. Codesenior.COM