Since I am trying to learn more about PHP I would like to add Pagination to array's
I have a JSON output that I can read and echo via a foreach. But I am not able to show 10 or 20 records.
I have used the code from this page:
But I miss the place where I can store the echo for the array.
$persons = '[
{"FrstName":"Henry","Middlename":"","LastName":"Walton","Online":true,"DeptId":"4"},
{"FrstName":"Klaus","Middlename":"","LastName":"Mikaelson","Online":true,"DeptId":"2"},
{"FrstName":"Kylo","Middlename":"","LastName":"Ren","Online":false,"DeptId":"4"},
{"FrstName":"Stan","Middlename":"","LastName":"Lee","Online":false,"DeptId":"3"},
{"FrstName":"Kevin","Middlename":"","LastName":"McNally","Online":false,"DeptId":"3"},
{"FrstName":"Katherine","Middlename":"","LastName":"Pierce","Online":false,"DeptId":"2"},
{"FrstName":"Clint","Middlename":"","LastName":"Barton","Online":true,"DeptId":"3"},
{"FrstName":"Avery","Middlename":"","LastName":"Walton","Online":true,"DeptId":"4"},
{"FrstName":"Peter","Middlename":"","LastName":"Kap","Online":true,"DeptId":"2"},
{"FrstName":"Denzo","Middlename":"","LastName":"Falc","Online":false,"DeptId":"4"},
{"FrstName":"Eveline","Middlename":"","LastName":"Benzel","Online":false,"DeptId":"3"},
{"FrstName":"Bill","Middlename":"","LastName":"Libuz","Online":false,"DeptId":"3"},
{"FrstName":"April","Middlename":"","LastName":"Gonzo","Online":false,"DeptId":"2"},
{"FrstName":"Harry","Middlename":"","LastName":"Geraldson","Online":true,"DeptId":"3"},
{"FrstName":"Heraldson","Middlename":"","LastName":"McGree","Online":false,"DeptId":"3"}
{"FrstName":"Abraham","Middlename":"","LastName":"Retz","Online":true,"DeptId":"4"},
{"FrstName":"June","Middlename":"","LastName":"Pharee","Online":true,"DeptId":"2"},
{"FrstName":"Anthony","Middlename":"","LastName":"Gonzales","Online":false,"DeptId":"4"},
{"FrstName":"Billy","Middlename":"","LastName":"Scott","Online":false,"DeptId":"3"},
{"FrstName":"Anika","Middlename":"","LastName":"Rose","Online":false,"DeptId":"3"},
{"FrstName":"Kristen","Middlename":"","LastName":"Fontana","Online":false,"DeptId":"2"},
{"FrstName":"Olivia","Middlename":"","LastName":"Menzel","Online":true,"DeptId":"3"},
{"FrstName":"Mark","Middlename":"van","LastName":"Gad","Online":false,"DeptId":"3"}
{"FrstName":"Hope","Middlename":"van","LastName":"Dyne","Online":false,"DeptId":"3"}
]';
$page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
$total = count( $persons ); //total items in array
$limit = 10; //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( $persons, $offset, $limit );
$link = 'index.php?page=%d';
$pagerContainer = '<div style="width: 300px;">';
if( $totalPages != 0 )
{
if( $page == 1 )
{
$pagerContainer .= '';
}
else
{
$pagerContainer .= sprintf( ' « prev page', $page - 1 );
}
$pagerContainer .= ' <span> page <strong>' . $page . '</strong> from ' . $totalPages . '</span>';
if( $page == $totalPages )
{
$pagerContainer .= '';
}
else
{
$pagerContainer .= sprintf( ' next page » ', $page + 1 );
}
}
$pagerContainer .= '</div>';
echo $pagerContainer;
I would like to know how I can fix this to use pagination for my array.
You need to json_decode() your JSON first to access array methods (count(), array_slice()). Also, your JSON data was invalid, missing some commas, fixed that.
<?php
$persons = '[
{"FrstName":"Henry","Middlename":"","LastName":"Walton","Online":true,"DeptId":"4"},
{"FrstName":"Klaus","Middlename":"","LastName":"Mikaelson","Online":true,"DeptId":"2"},
{"FrstName":"Kylo","Middlename":"","LastName":"Ren","Online":false,"DeptId":"4"},
{"FrstName":"Stan","Middlename":"","LastName":"Lee","Online":false,"DeptId":"3"},
{"FrstName":"Kevin","Middlename":"","LastName":"McNally","Online":false,"DeptId":"3"},
{"FrstName":"Katherine","Middlename":"","LastName":"Pierce","Online":false,"DeptId":"2"},
{"FrstName":"Clint","Middlename":"","LastName":"Barton","Online":true,"DeptId":"3"},
{"FrstName":"Avery","Middlename":"","LastName":"Walton","Online":true,"DeptId":"4"},
{"FrstName":"Peter","Middlename":"","LastName":"Kap","Online":true,"DeptId":"2"},
{"FrstName":"Denzo","Middlename":"","LastName":"Falc","Online":false,"DeptId":"4"},
{"FrstName":"Eveline","Middlename":"","LastName":"Benzel","Online":false,"DeptId":"3"},
{"FrstName":"Bill","Middlename":"","LastName":"Libuz","Online":false,"DeptId":"3"},
{"FrstName":"April","Middlename":"","LastName":"Gonzo","Online":false,"DeptId":"2"},
{"FrstName":"Harry","Middlename":"","LastName":"Geraldson","Online":true,"DeptId":"3"},
{"FrstName":"Heraldson","Middlename":"","LastName":"McGree","Online":false,"DeptId":"3"},
{"FrstName":"Abraham","Middlename":"","LastName":"Retz","Online":true,"DeptId":"4"},
{"FrstName":"June","Middlename":"","LastName":"Pharee","Online":true,"DeptId":"2"},
{"FrstName":"Anthony","Middlename":"","LastName":"Gonzales","Online":false,"DeptId":"4"},
{"FrstName":"Billy","Middlename":"","LastName":"Scott","Online":false,"DeptId":"3"},
{"FrstName":"Anika","Middlename":"","LastName":"Rose","Online":false,"DeptId":"3"},
{"FrstName":"Kristen","Middlename":"","LastName":"Fontana","Online":false,"DeptId":"2"},
{"FrstName":"Olivia","Middlename":"","LastName":"Menzel","Online":true,"DeptId":"3"},
{"FrstName":"Mark","Middlename":"van","LastName":"Gad","Online":false,"DeptId":"3"},
{"FrstName":"Hope","Middlename":"van","LastName":"Dyne","Online":false,"DeptId":"3"}
]';
// Make the JSON an array, so count() and array_slice() work
$persons = json_decode($persons, true);
$page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
$total = count( $persons ); //total items in array
// $limit = 10; //per page
// Set limit to 3 for testing:
$limit = 3;
$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
// Uncomment this for testing
// $page = 2;
$offset = ($page - 1) * $limit;
if( $offset < 0 ) $offset = 0;
$yourDataArray = array_slice( $persons, $offset, $limit );
$link = 'index.php?page=%d';
$pagerContainer = '<div style="width: 300px;">';
if( $totalPages != 0 )
{
if( $page == 1 )
{
$pagerContainer .= '';
}
else
{
$pagerContainer .= sprintf( ' « prev page', $page - 1 );
}
$pagerContainer .= ' <span> page <strong>' . $page . '</strong> from ' . $totalPages . '</span>';
if( $page == $totalPages )
{
$pagerContainer .= '';
}
else
{
$pagerContainer .= sprintf( ' next page » ', $page + 1 );
}
}
$pagerContainer .= '</div>';
echo $pagerContainer;
foreach($yourDataArray as $person) {
echo "\n";
echo "First name: " . $person['FrstName'];
echo " - Middle name: " . $person['Middlename'];
echo " - Last name: " . $person['LastName'];
echo " - Online: " . $person['Online'];
echo " - Dept: " . $person['DeptId'];
}
https://3v4l.org/8hR84
I created a pagination for an website file last year, and I've never really had to use it again.
But now I'll need to use it in a bigger application, and I'm afraid it won't behave really good with a huge amount of mysql rows, I don't know.
I'll post an example bellow and I'll be greatful if someone could tell me if I really need to upgrade the code.
/* Connection file */
include( 'config.php' );
/* Get the amount of all rows */
$mqc = "SELECT COUNT(*) AS total FROM table";
$mqe = mysql_query( $mqc );
$mqn = mysql_fetch_array( $mqe );
$total = $mqn['total'];
/* Rows per page */
$perPage = 10;
/* Range for the pagination */
$range = 5;
/* Actual page */
$page = isset( $_GET['page'] ) &&
is_numeric( $_GET['page'] ) &&
!empty( $_GET['page'] ) ?
( (int)$_GET['page'] * $range ) : 0;
/* Query */
$mqc = "SELECT * FROM table
LIMIT " . $page . ", " . $perPage . " ";
$mqe = mysql_query( $mqc );
/* Show results */
while( $l = mysql_fetch_object( $mqe ) )
{
echo $l->id . ' - ' . $l->title . '<br />';
}
/* Function for pagination */
function pages( $total = false, $perPage = false, $page = false, $range = false )
{
/* Number of pages */
$totalPages = ( ceil( $total / $perPage ) ) - 1;
/* The range * 2 */
$range = $range;
/* The real param $_GET['page'] */
$realGetPage = !empty( $page ) ? ( $page / $range ) : 0;
/* Show the first page */
if($realGetPage >= 7)
{
echo ' 1 ... ';
}
/* Loop to create the range */
for($i = ( $realGetPage - $range ); $i < ( $range + $realGetPage ); $i++)
{
/* Check out if the value isn't outta range */
if( $i > 0 && $i < $totalPages )
{
/* Highlight the actual page */
if( ( $i - 1 ) == $realGetPage )
{
echo ' [ ' . $i . ' ] ';
}
else
{
echo ' ' . $i . ' ';
}
}
/* Store the number of the last page */
$lastPage = ( $i + 1 );
}
/* Hack to show 5678910 */
if( $lastPage < ( $range * 2 ) + 1 )
{
for( $j = $lastPage; $j <= ( $range * 2 ); $j++ )
{
if( ($j - 1) == $realGetPage )
{
echo ' [ ' . $j . ' ] ';
}
else
{
echo ' ' . $j . ' ';
}
}
}
/* Last page */
echo ' ... ' . $totalPages . ' ';
}
/* Show the pagination */
pages( $total, $perPage, $page, $range );
What do you think?
From the information you provided in your comment, the performance issues you might have will not come from your PHP script. If you do multiple INNER JOINS on huge tables, this is exactly what will affect your performance. Some important things to look for:
Tables indexes (if you use filters, for your joins if you don't join on primary keys)
Optimizing your table / queries to decrease the number of JOINs
If you expect a lot of traffic, consider using caching for your queries (memcached for instance)
As Phil mentionned, the ORDER BY must be used to render a predictable result and could slow down your queries if the tables are used. You could pre-render ordered table using a cron job
Consider using denormalization to reduce the number of JOINS
The main thing I see missing is an ORDER BY clause. Without this, your paged results are (potentially) unpredictable.
Add an ORDER BY clause and make sure you have appropriate database indexes in place on the sortable column(s).
I'd also recommend switching to a more up-to-date database connection library with prepared statements and bound parameters. Please see PDO.