Wednesday, 14 August 2019

PHP / MySQL Select Data And Split On Pages

Show mysql 100 data row in 20page in php

<?php
error_reporting(0);
$servername = "localhost";
$username = "demo_user ";     // MySQL user name
$password = "123456789 ";    // MySQL password
$dbname = "demo_demo ";   // MySQL database name
$datatable = "register "; // MySQL table name
$results_per_page = 5; // number of results per page

// Create connection
$con = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}
?>
<?php
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $results_per_page;
$sql = "SELECT * FROM ".$datatable." ORDER BY ID ASC LIMIT $start_from, ".$results_per_page;
$rs_result = $con->query($sql);
?>
<table border="1" cellpadding="4">
<tr>
    <td bgcolor="#CCCCCC"><strong>ID</strong></td>
    <td bgcolor="#CCCCCC"><strong>Name</strong></td><td bgcolor="#CCCCCC"><strong>Phone</strong></td></tr>
<?php
 while($row = $rs_result->fetch_assoc()) {
?>
            <tr>
            <td><? echo $row["ID"]; ?></td>
            <td><? echo $row["Name"]; ?></td>
            <td><? echo $row["PhoneNumber"]; ?></td>
            </tr>
<?php
};
?>
</table>



<?php
$sql = "SELECT COUNT(ID) AS total FROM ".$datatable;
$result = $con->query($sql);
$row = $result->fetch_assoc();
$total_pages = ceil($row["total"] / $results_per_page); // calculate total pages with results
 
for ($i=1; $i<=$total_pages; $i++) {  // print links for all pages
            echo "<a href='1.php?page=".$i."'";
            if ($i==$page)  echo " class='curPage'";
            echo ">".$i."</a> ";
};
?>

No comments:

Post a Comment