user3055512 user3055512 - 2 months ago 9
PHP Question

hide some pages from php pagination system with sql

As the title says, I want to hide some pages from my pagination system. Because the table that it selects from is huge so it creates thousands of numbers.

So i want it to be something like
First Page 1 2 3 4 ... Last Page

And if i click on page for it should show this
First Page 1 2 3 4 5 6 7 8 ... Last page

And if i click on page 8 it should show this
First page ... 4 5 6 7 8 9 10 11 ... Last Page

So here is my php pagination script

<!DOCTYPE html>
<html>
<head>
<title>PHP Pagination</title>
</head>
<body>
<table align="center" border="2" cellpadding="3">
<tr>
<th>Name</th>
<th>quality</th>
<th>RequiredLevel</th>
</tr>
<?php
$con = mysqli_connect('127.0.0.1', 'root', 'ascent', 'world');

$per_page=500;

if (isset($_GET["page"])) {
$page = $_GET["page"];
}else{
$page=1;
}

$start_from = ($page-1) * $per_page;

$stmt = $con->prepare("SELECT name, Quality, RequiredLevel FROM item_template LIMIT $start_from, $per_page");

$stmt->execute();

$stmt->bind_result($name, $quality, $reqlvl);

$stmt->store_result();

while($stmt->fetch()) {

if($stmt->num_rows > 0) {
echo "<tr align='center'>";
echo "<td>$name</td>";
echo "<td>$quality</td>";
echo "<td>$reqlvl</td>";
echo "</tr>";
}
}
?>
</table>
<div>
<?php
$stmt = $con->prepare("SELECT * FROM item_template");

$stmt->execute();


$stmt->store_result();

if($stmt->num_rows > 0) {
$total_records = $stmt->num_rows;
$total_pages = ceil($total_records / $per_page);

echo "<center><a href='index.php?page=1'>First Page</a>";

for ($i=1; $i<=$total_pages; $i++) {
if($page < 4)
echo "<a href='index.php?page=$i'>$i</a> ";
}

echo "<a href='index.php?page=$total_pages'>Last Page</a></center> ";
}
?>
</div>
</body>
</html>

Answer

The solution for your pagination problem is this:

  • Get the current page number using the superglobal $_GET,

    // current page
    $current_page = isset($_GET['page']) ? $_GET['page'] : 1;
    
  • Find the superset range of pages, like 1-10, or 1-20 etc. For example, if $total_pages = 30 then this superset range would be 1-30. The code for this step is this:

    // superset range of pages
    $superset_range = range(1, $total_pages);
    
  • Find the subset range of pages to display, like 1-7, or 3-9 etc. For example, if $total_pages = 30 then this subset range would be 1-7, or 3-9, or 6-12 etc., it can be any consecutive seven pages between 1 and 30. Also, adjust this range whenever necessary. The code for this step is this:

    // adjust the range(if required)
    foreach($subset_range as $p){
        if($p < 1){
            array_shift($subset_range);
            if(in_array($subset_range[count($subset_range) - 1] + 1, $superset_range)){
                $subset_range[] = $subset_range[count($subset_range) - 1] + 1;
            }
        }elseif($p > $total_pages){
            array_pop($subset_range);
            if(in_array($subset_range[0] - 1, $superset_range)){
                array_unshift($subset_range, $subset_range[0] - 1);
            }
        }
    }
    
  • Finally, display the pagination links and dots accordingly. The code for this step is this:

    // display intermediate pagination links
    if($subset_range[0] > $superset_range[0]){
        echo " ...&nbsp;";
    }
    foreach($subset_range as $p){
        echo "<a href='index.php?page=$p'>$p</a>";
    
    }
    if($subset_range[count($subset_range) - 1] < $superset_range[count($superset_range) - 1]){
        echo "&nbsp;... ";
    }
    

So, here's the complete code:

// your code

$stmt = $con->prepare("SELECT * FROM item_template");
$stmt->execute();
$stmt->store_result();

if($stmt->num_rows > 0) {
    $total_records = $stmt->num_rows;
    $total_pages = ceil($total_records / $per_page);

    if($total_records > $per_page){
        echo "<center><a href='index.php?page=1'>First Page</a> ";

        // current page
        $current_page = isset($_GET['page']) ? $_GET['page'] : 1;

        // Superset range of pages
        $superset_range = range(1, $total_pages);

        // subset range of pages to display
        $subset_range = range($current_page - 3, $current_page + 3);

        // adjust the range(if required)
        foreach($subset_range as $p){
            if($p < 1){
                array_shift($subset_range);
                if(in_array($subset_range[count($subset_range) - 1] + 1, $superset_range)){
                    $subset_range[] = $subset_range[count($subset_range) - 1] + 1;
                }
            }elseif($p > $total_pages){
                array_pop($subset_range);
                if(in_array($subset_range[0] - 1, $superset_range)){
                    array_unshift($subset_range, $subset_range[0] - 1);
                }
            }
        }

        // display intermediate pagination links
        if($subset_range[0] > $superset_range[0]){
            echo " ...&nbsp;";
        }
        foreach($subset_range as $p){
            echo "<a href='index.php?page=$p'>$p</a>";

        }
        if($subset_range[count($subset_range) - 1] < $superset_range[count($superset_range) - 1]){
            echo "&nbsp;... ";
        }

        echo " <a href='index.php?page=$total_pages'>Last Page</a></center> ";  
    }    
} 
Comments