Jur Hillebrink Jur Hillebrink - 1 month ago 23
PHP Question

pagination array laravel redirecting

I am working with laravel and I work now with pagination for my array.

When I want to paginate three items from my array, It will output correctly. I want to see my another four items on te next page, I redirect to the same page and see the first three items from my array, but not the another four.

How my page works: First in an another view, I search on a item and make a GET request to my result controller and my result controller checks in the database the files and put it in an array. then, the result controller, returns the result page. with my pagination code.

This is my return view code:

return view('Documentatie.result')->with('zoekdocument', $zoekdocument)->with('searchdoc', $searchdoc)->with('fileopslag', $fileopslag); //geeft waardes mee.


the variable file $fileopslag is my array with the items.

this is my pagination code:

$page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
$total = count( $fileopslag ); //total items in array
$limit = 3; //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;

$show = array_slice( $fileopslag, $offset, $limit );


Then I show my results:

foreach($show as $shows){

<a href="/opendocument/{{basename($shows)}}" class="list-group-item"></a>

}


I see correctly three results. But when I want to the other page is see all files from my array, but not my another four.

this is my redirect code:

<?php


$link = '/result/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;


?>

Answer

What does not fit the standard functionality of pagination?

Paginating An Eloquent Model

$someUsers = User::where('votes', '>', 100)->paginate(15);

In view

<?php echo $users->render(); ?>

Doc

Hopefully this will solve your problem

Comments