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'm really frustrated.. I really need to show this table right. How can I set this code to show table vertically like this:
1 - 4 - 7
2 - 5 - 8
3 - 6 - 9
And not like this:
1 - 2 - 3 - 4
5 - 6 - 7 - 8
9 - ... ect
<?php
$PNG_WEB_DIR = plugin_dir_path( __FILE__ ).'temp/';
wp_mkdir_p($PNG_WEB_DIR);
$cols = $this->settings['cols'];
$rows = $this->settings['rows'];
$label_number = 0;
// die($_GET['order_ids']);
$order_ids = explode(',',$_GET['ids']);
$order_count = count($order_ids);
$labels_per_page = $cols*$rows;
$page_count = ceil(($order_count)/$labels_per_page);
for ($page=0; $page < $page_count; $page++) {
echo '<table class="address-labels" width="100%" height="100%" border="0" cellpadding="0">';
$last_height = 0;
$current_height = $current_width = 0;
$current_row = 0;
for ($label=0; $label < $labels_per_page; $label++) {
$label_number++;
$current_col = (($label_number-1) % $cols)+1;
if ($current_col == 1) {
$last_height = $current_height;
$last_width = 0;
$current_row++;
echo '<tr class="label-row">';
}
if ( $label_number > $this->offset && isset($order_ids[$label_number - $this->offset - 1]) ) {
$order_id = $order_ids[$label_number - $this->offset - 1];
} else {
$order_id = '';
}
$current_width = round( $current_col * (100/$cols) );
$width = $current_width - $last_width;
$last_width = $current_width;
$current_height = round( $current_row * (100/$rows) );
$height = $current_height - $last_height;
printf('<td width="%s%%" height="%s%%" class="label"><div class="label-wrapper">', $width, $height);
// because we are also looping through the empty labels,
// we need to check if there's an order for this label
if (!empty($order_id)) {
// get label data from order
$order = new WC_Order( $order_id );
// replace placeholders
$label_data = isset($this->settings['address_data'])? nl2br( $this->settings['address_data'] ): '[shipping_address]';
$label_data = $this->make_replacements( $label_data, $order );
echo '<div class="address-block">';
echo '<div class="addrress_show">';
// process label template to display content
echo $label_data;
echo '</div>';
echo '<div class="clearb"></div>';
echo '</div>';
} else {
echo ' ';
}
echo '</div></td>';
if ($current_col == $cols) {
echo '</tr>';
}
}
echo '</table>';
}
// shpt_after_page hook
do_action( 'shpt_after_page' );
?>
Since I can't know your raw data structure which you use to loop through it and produce the table, I presumed it as one-dimensional array and you want to print the table as 3 columns as you said, to see in action here's a PHP Fiddle - hit run to execute it - this will work for any array length as long as columns is 3, the rows is dynamic
PHP: Updated for naming purpose
<?php
$table = '<table class="address-labels" width="30%" border="0" cellpadding="0">';
$dataArr = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, 17);
$colNum = 3;
$rowNum = ceil(count($dataArr)/$colNum);
for($i=0; $i < $rowNum; $i++){
$x1 = isset( $dataArr[$i] ) ? $dataArr[$i] : '-';
$x2 = isset( $dataArr[$i + $rowNum] ) ? $dataArr[$i + $rowNum] : '-';
$x3 = isset( $dataArr[$i + 2 * $rowNum] ) ? $dataArr[$i + 2 * $rowNum] : '-';
$table .= '<tr><td>' . $x1 . '</td><td>' . $x2 . '</td><td>' . $x3 . '</td></tr>';
}
$table .= '</table>';
echo $table;
?>
EDIT:
Now if you want to change the column number, like set it to 5 for example, you need to do the following:
1st - set $colNum = 5;
2nd - add more variables to hold the values like, *considering it is 5 columns:
$x4 = isset( $dataArr[$i + 3 * $rowNum] ) ? $dataArr[$i + 3 * $rowNum] : '-';
$x5 = isset( $dataArr[$i + 4 * $rowNum] ) ? $dataArr[$i + 4 * $rowNum] : '-';
and don't forget to increase the multiplier * $rowNum
then for each extra column you add you need to add <td></td> like:
'<td>' . $x4 . '</td><td>' . '</td><td>' . $x5 . '</td><td>' . '</td>'
Here's another PHP Fiddle showing the above for 5 columns.
Try this. It's in pure php
<?php
$cols = 3;
$data = range(1,29);
// classic
$index = 1;
foreach($data as $el)
{
echo $el.' '; if ($index % $cols ==0) echo '<br/>';
$index++;
}
echo '<br/><br/>';
// special
$rows = $cols ;
$cols = floor(count($data)/$cols);
for($row = 0 ; $row < $rows; $row++){
for ($index = $row; $index < count($data); $index += $rows)
echo $data[$index].' ';
echo '<br/>';
}