How to Add PHP Pagination in Array'S

How to add PHP pagination in array's

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"> « 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 » </a>', $page + 1 );
}
}
$pagerContainer .= '</div>';

echo $pagerContainer;

Pagination with array items in php

Assuming your $offset changes accordingly to page number, it should be

$this->data['array'] = array_slice($array, $offset, $per_page); 

because array_slice second argument is offset and third is length.

How to use pagination on an array of arrays in Laravel

You can not paginate an array. You paginate the output of the query which is stored in an array but not the array itself.

That means that your 3 nested arrays should already be paginated before inserted in the parent array.

After that you loop around them and you get the links field. This is your pagination info.

Applying pagination for array in php

Below is the simplified code according to your need, you just need to loop through your array and make those TR dynamic

You can download the js and css from here

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, maximum-scale=1"/>
<title>Pagination Example</title>

<link href="css/simplePagination.css" type="text/css" rel="stylesheet"/>
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/jquery.simplePagination.js"></script>

<script>
jQuery(function($) {
var items = $("#content tbody tr");

var numItems = items.length;
var perPage = 2;

// only show the first 2 (or "first per_page") items initially
items.slice(perPage).hide();

// now setup pagination
$("#pagination").pagination({
items: numItems,
itemsOnPage: perPage,
cssStyle: "light-theme",
onPageClick: function(pageNumber) { // this is where the magic happens
// someone changed page, lets hide/show trs appropriately
var showFrom = perPage * (pageNumber - 1);
var showTo = showFrom + perPage;

items.hide() // first hide everything, then show for the new page
.slice(showFrom, showTo).show();
}
});
});
</script>
</head>
<body>
<h1>Pagination Example</h1>
<div id="pagination"></div>
<table id="content">
<tbody>
<tr>
<td>Window</td>
<td>John</td>
</tr>
<tr>
<td>Door</td>
<td>Chris</td>
</tr>
<tr>
<td>Floor</td>
<td>Michael</td>
</tr>
<tr>
<td>Car</td>
<td>James</td>
</tr>
<tr>
<td>Bike</td>
<td>Steven</td>
</tr>
</tbody>
</table>
</body>
</html>

Create pagination on an existing array

To take a part (or a slice) or array, use array_slice and define it's parameters as:

$pageSize = 9;
$startPosition = ($_GET['pagenum'] - 1) * $pageSize;
$slice = array_slice(
$fullArray,
$startPosition,
$pageSize
);

To split array into parts (or chunks) use array_chunk:

$pageSize = 9;
$chunks = array_chunk(
$fullArray,
$pageSize
);
// after that you can get required chunk by index - $chunks[0], $chunks[1], etc

How to do a pagination from array?

Have a look at this:

    function paganation($display_array, $page) {
global $show_per_page;

$page = $page < 1 ? 1 : $page;

// start position in the $display_array
// +1 is to account for total values.
$start = ($page - 1) * ($show_per_page + 1);
$offset = $show_per_page + 1;

$outArray = array_slice($display_array, $start, $offset);

var_dump($outArray);
}

$show_per_page = 2;

paganation($display_array, 1);
paganation($display_array, 2);

$show_per_page = 3;
paganation($display_array, 1);
paganation($display_array, 2);

The output is:

// when $show_per_page = 2;
array
0 => string '0602 xxx2' (length=9)
1 => string '0602 xxx3' (length=9)
2 => int 5
array
0 => string '0602 xxx3' (length=9)
1 => string '0602 saa4' (length=9)
2 => int 7

// when $show_per_page = 3;
array
0 => string '0602 xxx2' (length=9)
1 => string '0602 xxx3' (length=9)
2 => int 5
3 => string '0602 xxx3' (length=9)
array
0 => string '0602 saa4' (length=9)
1 => int 7

The output for $show_per_page = 3 is different than yours, but I'm not sure what you expect? You want to fetch everything that is left (i.e. '0602 saa4' and 7) plus one previous element (i.e. '0602 xxx3')?

Array Pagination

First of all your JSON has some small errors. It is not valid JSON.
Following is the correct JSON.

{
"blogs": {
"1": {
"slug": "blog\/cfsdgfdgfd",
"cover_image": "cfsdgfdgfd.jpg",
"author": {
"name": "dsfdsaf",
"image": "fdsafas",
"email": "fdsafsa"
},
"heading": "fdgdfg",
"excerpt": "sdfdsfdsaf",
"date_added": "2019-04-25T12:21:31+10:00",
"date_modified": "2021-12-07T14:05:12+10:00",
"visible": "1",
"comments": "0",
"status": "1"
},
"2": {
"slug": "blog\/hxgch",
"cover_image": "fdghhfd.jpg",
"author": {
"name": "fdghf",
"image": "zhd",
"email": "kjhgk"
},
"heading": "kjhkhjg",
"excerpt": "hgfdhfd",
"date_added": "2019-05-09T13:31:04+10:00",
"date_modified": "2021-12-07T11:40:49+10:00",
"visible": "1",
"comments": "0",
"status": "1"
}
}
}

In your code you are considering the json_decode values as objects but I json_decode it as an associate array as I think it is easier to work with.

Following is the code with pagination.

$fileData = file_get_contents("Path to your JSON file");
$jsonData = json_decode($fileData, true);
$itemsPerPage = 1;
$nextPage = 1; // You can access this as $currentPage = $_GET['page'];
$counter = 0;

foreach ($jsonData['blogs'] as $jsonDatum){

if ($jsonDatum['status'] === '1' && $jsonDatum['visible'] === '1' && strtotime($jsonDatum['date_added']) <= strtotime(date('r'))){

$lastIndex = ($itemsPerPage * $nextPage);
$firstIndex = (($itemsPerPage * $nextPage) - $itemsPerPage);

if ($firstIndex <= $counter && $counter < $lastIndex) {

// Your HTML code comes here
// echo("<article></article>");

// access the values using array indexes
$cover_image = $jsonDatum['cover_image'];
$authorName = $jsonDatum['author']['name'];

}
}

$counter++;
}

Simple paging from Single Array

Yes, array_slice is exactly what you want. It's the most efficient way to do this. Here's what you need.

function get_page(array $input, $pageNum, $perPage) {
$start = ($pageNum-1) * $perPage;
$end = $start + $perPage;
$count = count($input);

// Conditionally return results
if ($start < 0 || $count <= $start) {
// Page is out of range
return array();
} else if ($count <= $end) {
// Partially-filled page
return array_slice($input, $start);
} else {
// Full page
return array_slice($input, $start, $end - $start);
}
}


Related Topics



Leave a reply



Submit