Logic to get offset for mysql query to fetch data.

$offset = ($cur_page - 1) * $limit;

$cur_page = Here page is the number of page which is currently showing.

$limit = Limit  is the number of rows fetching from the database.

 

Logic to create paging.

<?php
##These variables have to assign with actual data
## $total_rows ## $limit ## $cur_page

$last = ceil($total_rows / $limit);
$links = 1;

$start = ( ( $cur_page - $links ) > 0 ) ? $cur_page - $links : 1;
$end = ( ( $cur_page + $links ) < $last ) ? $cur_page + $links : $last;

$html = '<ul class="' . $list_class . '">';

$class = ( $cur_page == 1 ) ? "disabled-pagi" : "";
$html .= '<li class="' . $class . '"><a href="?page=' . ( $cur_page - 1 ) . '">&laquo;</a></li>';

if ($start > 1) {
    $html .= '<li><a href="?page=1">1</a></li>';
    $html .= '<li class="disabled-pagi"><span>...</span></li>';
}

for ($i = $start; $i <= $end; $i++) {
    $class = ( $cur_page == $i ) ? "active-pagi" : "";
    $html .= '<li class="' . $class . '"><a href="?page=' . $i . '">' . $i . '</a></li>';
}

if ($end < $last) {
    $html .= '<li class="disabled-pagi"><span>...</span></li>';
    $html .= '<li><a href="?page=' . $last . '">' . $last . '</a></li>';
}

$class = ( $cur_page == $last ) ? "disabled-pagi" : "";
$html .= '<li class="' . $class . '"><a href="?page=' . ( $cur_page + 1 ) . '">&raquo;</a></li>';

$html .= '</ul>';

?>

Another Login to create paging :-

<?php
##These variables have to assign with actual data
## $total_rows ## $limit ## $cur_page

//only change links(number of page links to show) in only odd numbers.
$links = 5;
$url = '?page=';
######################## PAGING LOGIC START ##########################
$last = ceil($total_rows / $limit);
$middle_page_no = ceil($links / 2);
$left_right_side_pages_no = $middle_page_no - 1;

//calculating start
$start = 1;
if ($cur_page > $middle_page_no)
    $start = $cur_page - $left_right_side_pages_no;


//calculating end
$end = $cur_page + $left_right_side_pages_no;
if ($cur_page < $middle_page_no) {
    $tmp = $middle_page_no - $cur_page;
    $end = $end + $tmp;
}
if ($end > $last)
    $end = $last;

//Re-calculating start according to end.
$tmp = $last - $cur_page;
if ($tmp == 0)
    $start = $start - $left_right_side_pages_no;
elseif ($tmp < $left_right_side_pages_no)
    $start = $start - ($left_right_side_pages_no - $tmp);
$start = ($start < 1) ? 1 : $start;
######################## PAGING LOGIC END ##########################
?>
<ul class="pagi-ul">

    <?php if ($start == 1) : ?>
        <a href="javascript:void(0)"><li class="pagi-disabled"> << </li></a>
        <a href="javascript:void(0)"><li class="pagi-disabled"> < </li></a>
    <?php else : ?>
        <a href="<?= $url ?>1"><li title="First"> << </li>
        <a href="<?= $url . ($cur_page - 1) ?>"><li title="Previous"> < </li></a>
    <?php endif; ?>


    <?php
       for ($i = $start; $i <= $end; $i++) :
        $url2 = ($cur_page == $i) ? 'javascript:void(0)' : $url . $i;
    ?>
        <a href="<?= $url2; ?>">
            <li class="<?= $cur_page == $i ? 'pagi-active' : '' ?>"> <?= $i ?> </li>
        </a>
    <?php endfor; ?>


    <?php
    if ($end < $links):
        for ($i = ($end + 1); $i <= $links; $i++) :
            ?>
            <a href="javascript:void(0)">
                <li class="pagi-disabled"> <?= $i ?> </li>
            </a>
            <?php
        endfor;
    endif;
    ?>


    <?php if ($end == $last) : ?>
        <a href="javascript:void(0)"><li class="pagi-disabled"> > </li>
        <a href="javascript:void(0)"><li class="pagi-disabled"> >> </li></a>
    <?php else : ?>
        <a href="?page=<?= $cur_page + 1 ?>"><li title="Next"> > </li>
        <a href="?page=<?= $last ?>"><li title="Last"> >> </li></a>
    <?php endif; ?>

</ul>

<!--css for paging-->
<style>
    .pagi-ul li{
        list-style: none; 
        float: left; margin-right: 10px; 
        border: 1px #000 solid; padding: 0px 10px; 
        background-color: #C1C682;
    }
    .pagi-ul li.pagi-disabled{
        background-color: #F3EEEA !important; 
        cursor: not-allowed;
        opacity: 0.3;
    }
    .pagi-ul li.pagi-active{background-color: #E37722 !important; cursor: cell;}
</style>

 

  • PHP
  • pagination