Pagination in php mysql step by step

Pagination.php Preview



Code Preview





Pagination.php

<?php
if(isset($_GET['id'])){
    $p = $_GET['id'];
    $p *= 5; // each page show 5 records
}else{
    $p = 0;
}
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'mydb';

$con = mysqli_connect($host, $user, $pass, $db);
if(!$con){
    die('Connection Error!');
}
//  *******  Count Records ********
$sql = "Select count(*) as num from image";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result);
$count = $row['num'];
if($count > 5){
    $pages = $count/5;
    if((int)(end(explode('.', $pages))) > 0){
        $pages = (int)($count/5 + 1); // Each Page Show 5 Records
    }
}else{
    $pages = 1;
}
// ****** Show Pages Numbers *********
for($i=0;$i< $pages;$i++){
        echo '<a href="Paging.php?id='.$i.'">Page:'.$i.'</a>';
        echo "\t";
}
echo '<br><br>';
// ******   Show Records  ********
$sql1 = "select * from image limit {$p},5";
$rel = mysqli_query($con, $sql1);
while ($row = mysqli_fetch_array($rel)){
    $img = base64_encode( $row['img'] );              
                 ?>
<img src="data:image/jpeg;base64,<?php echo $img; ?>" height="300" width="300">
<p><b>Image Name :</b> <?php echo $row['name']; ?></p>          
    <?php
}

No comments:

Post a Comment