Simple paging with PHP

I would like to implement paging in PHP. I have a result set from db, let it be an array. I need a paging that should display 4 entries per page, and page numbers should be as follows

"<1,2,3 ..... 20 →"

and when you select page 2, the format should be as follows

"<3,4,5 ..... 20 →"

Can you guys suggest me some swap concepts to implement this?

+3
source share
5 answers

In fact, you can use the sentence LIMITin SQL.

Here's a decent tutorial to get you started.

+3
source

Take a look at PEAR :: Pager .

+1

, , , . .

, page = 2 .

$pagenum=(int)$_GET['page'];
if($pagenum<1){ $pagenum=1; }//perform a sanity check. You might also query to find the max page number and see that it not higher than that.

pagenum SQL- , .

, SQL, , , , int - ( (int)). .

, 4 . LIMIT LIMIT OFFSET . , - , - .

$offset=$pagenum*4-4;//this means for page 1, start on 0, page 2 starts on 4, etc.
$sql="select * from the_table limit $offset,4";

, . - . , , , .

for($i=pagenum+1;$i<20;$i++){
  if($i<$pagenum+4){  ?>
  <a href='stuff.php?page=<?php echo $i;?>'><?php echo $i;?></a>
  <?php } elseif($i==$pagenum+4) { ?>
  ... <?php } elseif($i==20){ ?>
  <a href='stuff.php?page=20'>20</a>
  <?php } ?>
+1

. PEAR:: Pager, Zend_Paginator Zend Framework.

, Github - , .

0

, Db, u , , pageSize, PageNumber, sortOrder. sql

SELECT * FROM your_table WHERE condition = true ORDER BY some_field LIMIT 100, 10
0

Source: https://habr.com/ru/post/1730330/


All Articles