-3

I would like to ask if how to do simple paging and pagination like GOOGLE.

should I use javascript?

should I use bootstrap?

This is my code for my pagination and I have 30 page buttons right now with 9 records per page

and all of them are present on the page.

$results_per_page = 9;
// find out the number of results stored in database
$result = mysqli_query($link, $query);
$number_of_results = mysqli_num_rows($result);
// determine number of total pages available
$number_of_pages = ceil($number_of_results/$results_per_page);
// determine which page number visitor is currently on
if (!isset($_GET['page'])) {
    $page = 1;
} else {
    $page = $_GET['page'];
}

// determine the sql LIMIT starting number for the results on the displaying page

$this_page_first_result = ($page-1)*$results_per_page;
// retrieve selected results from database and display them on page
$sql='SELECT * FROM policy LIMIT ' . $this_page_first_result . ',' .  $results_per_page;

$result = mysqli_query($link, $sql);

// display the links to the pages
?>
<form method="post">
<?php
for ($page=1;$page<=$number_of_pages;$page++) {
?>
    <input class="btn btn-success" type="submit" value="<?php echo $page;?>" name="page" >
<?php  
}
?>

</form>

<?php
$page = 0;
?>

I want to hide other buttons while the 10 buttons are shown.

Thank you in advance ^_^

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Hershey
  • 1
  • 1
  • 1
    Please go read [ask]. We expect you to show a bit more effort here, than _just_ telling us what you “want”. What have you researched, what have you tried already, with what results? – 04FS Sep 27 '19 at 08:30
  • Bootstrap is a presentation tool, so it would not help in any obvious way in pagination – RiggsFolly Sep 27 '19 at 08:32
  • Your script is open to [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) attack. Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) You should consider using [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's instead of concatenated values – RiggsFolly Sep 27 '19 at 08:34

1 Answers1

0

You could limit the amount of pages to display left and right of the current page. Let's assume you are on page 33/100 and want this shown:

<First> ... 30 31 32 [33] 34 35 36 37 38 39 40 ... <Last>

That is 3 pages to the left and 7 to the right

To achieve this use somthing like this:

$pageDisplayToLeft = 3;
$pageDisplayToRight = 7;
$pagesTotal = 100;
$currentPage = 33;

echo '<First>';

if(($currentPage - $pageDisplayToLeft) > 1) {
    echo ' ... ';
}

$pageDisplay = max(1, $currentPage - $pageDisplayToLeft);
while($pageDisplay < $currentPage) {
    echo $pageDisplay . ' ';
    $pageDisplay++;
}
echo '[' . $currentPage . '] ';

$pageDisplay = min($pagesTotal, $currentPage + 1);
while($pageDisplay <= min($currentPage + $pageDisplayToRight, $pagesTotal)) {
    echo $pageDisplay . ' ';
    $pageDisplay++;
}


if(($currentPage + $pageDisplayToRight) < $pagesTotal) {
    echo ' ... ';
}

echo '<Last>';

Simply insert html tags, link the pages and add dynamic values for $pagesTotal & $currentPage. (e.g. $currentPage = $_GET['page'];).

There are some special cases you need to handle (e.g. Don't display <First> if page 1 is visible in the row) but you get the idea!

Hope this will help you!

Jonathan
  • 1,955
  • 5
  • 30
  • 50