Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add PHP pagination in array's

I've been trying a lot of ways to add PHP pagination. I have tried searching and trying to figure other ways of implementing the pagination but none of them work.

Here's how I created the Index page:

<?php
$menuItems = array(
    "post1" => array(
        "title" => "Sample Title",
        "utime" => "M/d/Y",
        "content" => "<p>Body of the post</p>"
    ),

    "post2" => array(
        "title" => "Another Sample Title",
        "utime" => "M/d/Y",
        "content" => "<p>Content goes here...</p>"
    ),
);

foreach ($menuItems as $contItem => $item) {
?>
<li>
     <a href="dish.php?item=<?php echo $contItem; ?>">
         <h1><?php echo $item["title"]; ?></h1>
         <small><?php echo $item["utime"]; ?></small>
     </a>
</li>
<?php } ?>

I would like to know how I can paginate the the array list. Thanks!

like image 823
Arqetech Avatar asked Oct 19 '14 14:10

Arqetech


People also ask

How can I code pagination in PHP?

The steps to follow to implement pagination with PHP and MySQL are: STEP1: Create a large-scale database. STEP2: Create a table and insert the list of records in it. STEP3: Create a link for pagination to segregate/distribute the data over several different pages and add it to the bottom of the table created in step 2.

How can I set pagination limit in PHP?

Try this : <? php $link = ""; $page = $_GET['pg']; // your current page // $pages=20; // Total number of pages $limit=5 ; // May be what you are looking for if ($pages >=1 && $page <= $pages) { $counter = 1; $link = ""; if ($page > ($limit/2)) { $link . = "<a href=\"?

How can I get pagination in laravel?

There are several ways to paginate items. The simplest is by using the paginate method on the query builder or an Eloquent query. The paginate method provided by Laravel automatically takes care of setting the proper limit and offset based on the current page being viewed by the user.

How can I get current page number in pagination PHP?

$currentPage = ceil(($startIndex - 1) / $itemsPerPage) + 1; I used ceil() to make sure you have an integer number, which is rounded up so the current page number is correct.


2 Answers

u can use simple PHP function called array_slice()

$menuItems = array_slice( $menuItems, 0, 10 ); 

show first 10 items.

$menuItems = array_slice( $menuItems, 10, 10 );

show next 10 items.

UPDATE:

$page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
$total = count( $yourDataArray ); //total items in array    
$limit = 20; //per page    
$totalPages = ceil( $total/ $limit ); //calculate total pages
$page = max($page, 1); //get 1 page when $_GET['page'] <= 0
$page = min($page, $totalPages); //get last page when $_GET['page'] > $totalPages
$offset = ($page - 1) * $limit;
if( $offset < 0 ) $offset = 0;

$yourDataArray = array_slice( $yourDataArray, $offset, $limit );

UPDATE#2:

Example of pagination:

$link = 'index.php?page=%d';
$pagerContainer = '<div style="width: 300px;">';   
if( $totalPages != 0 ) 
{
  if( $page == 1 ) 
  { 
    $pagerContainer .= ''; 
  } 
  else 
  { 
    $pagerContainer .= sprintf( '<a href="' . $link . '" style="color: #c00"> &#171; prev page</a>', $page - 1 ); 
  }
  $pagerContainer .= ' <span> page <strong>' . $page . '</strong> from ' . $totalPages . '</span>'; 
  if( $page == $totalPages ) 
  { 
    $pagerContainer .= ''; 
  }
  else 
  { 
    $pagerContainer .= sprintf( '<a href="' . $link . '" style="color: #c00"> next page &#187; </a>', $page + 1 ); 
  }           
}                   
$pagerContainer .= '</div>';

echo $pagerContainer;
like image 130
trzyeM- Avatar answered Nov 15 '22 14:11

trzyeM-


Another viable option is to use array_chunk():

$pagedArray = array_chunk($originalArray, 10, true);
$nthPage = $pagedArray[$pageNumber];
like image 33
Cranio Avatar answered Nov 15 '22 14:11

Cranio