article

Tuesday, September 24, 2019

Pagination with PHP and Mysqli

Pagination with PHP and Mysqli
<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pagination with PHP and Mysqli</title>
</head>
<body>
<div align="center" style="font-size:24px;color:#cc0000;font-weight:bold">Pagination with PHP and Mysqli</div>
<div id="container">
<?php
$page = (isset($_GET['page']))?$_GET['page']:'';
if ($page==''){
 $page = "1";
}else{
 $page = $_GET['page'];
}
include"dbcon.php";
$cur_page = $page;
$page -= 1;
$per_page = 11; 
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
$msg = "";
$query_pag_data = "SELECT * from country LIMIT $start, $per_page";
$result_pag_data = mysqli_query($conn, $query_pag_data);
while($row = mysqli_fetch_assoc($result_pag_data)) {
 $htmlmsg=htmlentities($row['country']); 
    $msg .= "<li><b>" . $row['id'] . "</b> " . $htmlmsg . "</li>";
}
$msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data

$query_pag_num = mysqli_query($conn,"SELECT COUNT(*) AS mycount FROM country" ) or die(mysqli_error($this->dblink));
$res = mysqli_fetch_object($query_pag_num);
$count = $res->mycount;
$no_of_paginations = ceil($count / $per_page);

// ---------------Calculating the starting and endign values for the loop----------------------------------- 
if ($cur_page >= 7) {
    $start_loop = $cur_page - 3;
    if ($no_of_paginations > $cur_page + 3)
        $end_loop = $cur_page + 3;
    else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) {
        $start_loop = $no_of_paginations - 6;
        $end_loop = $no_of_paginations;
    } else {
        $end_loop = $no_of_paginations;
    }
} else {
    $start_loop = 1;
    if ($no_of_paginations > 7)
        $end_loop = 7;
    else
        $end_loop = $no_of_paginations;
}
//----------------------------------------------------------------------------------------------------------- 
$msg .= "<div class='pagination'><ul>";

// FOR ENABLING THE FIRST BUTTON
if ($first_btn && $cur_page > 1) {
    $msg .= "<li p='1' class='active'><a href='?page=1'>First</a></li>";
} else if ($first_btn) {
    $msg .= "<li p='1' class='inactive'><a href='?page=1'>First</a></li>";
}
// FOR ENABLING THE PREVIOUS BUTTON
if ($previous_btn && $cur_page > 1) {
    $pre = $cur_page - 1;
    $msg .= "<li p='$pre' class='active'><a href='?page=$pre'>Previous</a></li>";
} else if ($previous_btn) {
    $msg .= "<li class='inactive'>Previous</li>";
}
for ($i = $start_loop; $i <= $end_loop; $i++) {
    if ($cur_page == $i)
        $msg .= "<li p='$i' style='color:#fff;background-color:#70BA4C;' class='active'><a href='?page=$i'>{$i}</a></li>";
    else
        $msg .= "<li p='$i' class='active'><a href='?page={$i}'>{$i}</a></li>";
}
// TO ENABLE THE NEXT BUTTON
if ($next_btn && $cur_page < $no_of_paginations) {
    $nex = $cur_page + 1;
    $msg .= "<li p='$nex' class='active'><a href='?page=$nex'>Next</a></li>";
} else if ($next_btn) {
    $msg .= "<li class='inactive'>Next</li>";
}
// TO ENABLE THE END BUTTON
if ($last_btn && $cur_page < $no_of_paginations) {
    $msg .= "<li p='$no_of_paginations' class='active'><a href='?page=$no_of_paginations'>Last</a></li>";
} else if ($last_btn) {
    $msg .= "<li p='$no_of_paginations' class='inactive'><a href='?page=$no_of_paginations'>Last</a></li>";
}
$total_string = "<span class='total' a='$no_of_paginations'>Page <b>" . $cur_page . "</b> of <b>$no_of_paginations</b></span>";
$msg = $msg . "</ul>" . $total_string . "</div>";  // Content for pagination 
echo $msg;
?>
</div>
<style type="text/css">
            body{
                width: 800px;
                margin: 0 auto;
                padding: 0;
            }
   a {text-decoration:none;}
   #container {margin-bottom:20px;}
   #container .data{
    list-style-type: none;
    margin: 0;
    padding: 0;
   }
            #container .data ul li{
                list-style: none;
                background-color:lightyellow;
    border:1px solid #FFDA5B;
    padding:10px;
    margin-bottom:5px;
            }
   #container .data ul li:hover{
                background-color:#70BA4C;
    border:1px solid #448B22;
            }
   #container .data ul li b {
                -moz-border-radius:12px;
    -webkit-border-radius:12px;
    border-radius:32px;
    background-color:#70BA4C;
    border:1px solid #448B22;
    color:#FFFFFF;padding:6px;margin-right:5px;
    text-shadow:1px 1px 0 green;
   }
            #container .pagination ul li.inactive,
            #container .pagination ul li.inactive:hover{
                background-color:#ededed;
                color:#bababa;
                border:1px solid #bababa;
                cursor: default;
            }
            #container .pagination{
                width: 800px;
                height: 25px;
            }
            #container .pagination ul li{
                list-style: none;
                float: left;
                border: 1px solid #006699;
                padding: 4px 8px 4px 8px;
                margin: 0 3px 0 3px;
                font-family: arial;
                font-size: 14px;
                color: #006699;
                font-weight: bold;
                background-color: #f2f2f2;
            }
            #container .pagination ul li:hover{
                color: #fff;
                background-color: #006699;
                cursor: pointer;
            }
   .total
   {
   float:right;font-family:arial;color:#999;
   }
</style>  
</body>
</html>
//dbcon.php
<?php
$conn = new mysqli('localhost','root','','testingdb');
if ($conn->connect_error) {
    die('Error : ('. $conn->connect_errno .') '. $conn->connect_error);
}
?>
Download SQL file Here

Related Post