PHPLover PHPLover - 4 years ago 151
PHP Question

How to get/return the array elements in parts as per the current page number from pagination function?

I'm using PHP for my website. I've an dynamic associative array which may contain any no. of elements. Each of the elements in this array is another array. In order to limit the no. of array elements to be shown to the user on single page I wrote a pagination function.

It's working fine and returning me the necessary information of current page, next page, records per page, etc. The issue I'm facing is I'm only getting this pagination information and not the array element which I want to display on a single web page to the user.

The code I've written for pagination is as follows:

function paginate($page, $total, $per_page) {
if(!is_numeric($page)) {
$page = 1;
}

if(!is_numeric($per_page)) {
$per_page = 10;
}

if($page > ceil($total / $per_page))
$page = 1;

if($page == "" || $page == 0) {
$page = 1;
$start = 0;
$end = $per_page;
} else {
$start = ($page * $per_page) - ($per_page);
$end = $per_page;
}

$prev_page = "";
$next_page = "";
$all_pages = array();
$selected = "";
$enabled = false;

if($total > $per_page) {
$enabled = true;
$prev = $page - 1;
$prev_page = ($prev == 0) ? 0 : $prev;
$next = $page + 1;
$total_pages = ceil($total/$per_page);

$next_page = ($next <= $total_pages) ? $next : 0;

for($x=1;$x<=$total_pages;$x++) {
$all_pages[] = $x;
$selected = ($x == $page) ? $x : $selected;
}
}

return array(
"per_page" => $per_page,
"page" => $page,
"prev_page" => $prev_page,
"all_pages" => $all_pages,
"next_page" => $next_page,
"selected" => $selected,
"start" => $start,
"end" => $end,
"enabled" => $enabled
);
}


The function call is as follows:

$required_data = $getAllRequiredData->GetDataEntries();
$element_count = count($required_data);
$single_page_data = array();
$single_page_data = paginate(1, $element_count, 10);
print_r($single_page_data);


Suppose I'm having 40 elements in an array and I want to display only 10 elements per page.
After the function call I get following output:

/*You can see that the function works fine and returns the necessary pagination data but
it's not returning the array elements to be shown on respective page*/
Array
(
[per_page] => 10
[page] => 1
[prev_page] => 0
[all_pages] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)

[next_page] => 2
[selected] => 1
[start] => 0
[end] => 10
[enabled] => 1
)


For your reference I'm putting below two elements from array
$required_data
:

Array
(
[0] => Array
(
[id] => 49
[invoice_number] =>
[invoice_date] => 2014-07-09
[status_message] => 2014-07-11 01:29:05 : approved##2014-07-11 00:08:18 : approved##This is rejected due to date is out of date
[product_details] => Array
(
[0] => Array
(
[product_id] => 30
[product_name] => Louis Jadot Beaujolais Village
[product_code] => JD

)

)

)

[1] => Array
(
[id] => 48
[invoice_number] =>
[invoice_date] => 2014-07-05
[status_message] => done
[product_details] => Array
(
[0] => Array
(
[product_id] => 12
[product_name] => Riesling
[product_code] => SIGN

)

)

)
)

Answer Source

It looks like you just need to extract the relevant parts of the array. See if this works

$required_data = $getAllRequiredData->GetDataEntries();

// sort $required_data if needed

$element_count = count($required_data);

$p = paginate(1, $element_count, 10);

$start = $p['per_page'] * $p['page'];
$qty = $p['per_page'];

if ($start + $qty > $element_count) {
  $qty = $element_count - $start;
}

$single_page_data = array_slice($required_data, $start, $qty);

print_r($single_page_data);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download