Algorithm to separate pagination display by ...? - php

Have an $A is the data for pagination:
$A = array(
0=>array(
0=>1,
1=>2
),
1=>array(
0=>3,
1=>5,
2=>2
),
2=>array(
0=>3,
1=>1,
2=>6,
3=>6
)
);
Anybody could help me to get the expected ouput (output this "...." more) the most important ?
.... told that it is still more element need to display next page.
or it the remain element from previous page.
There are 09 elements of $A to display ,So
I set
$show_per_page = 3;
Output (for the first page):
1
2
Total:3
3
....//output this "...." more
Output (for the second page):
....//output this "...." continue from first page
5
2
Total:10
3
.... //output this "...." more
Output (for the third page):
.... //output this "...." continue from second page
1
6
6
Total:16
if I set
$show_per_page = 5;
Output (for the first page):
1
2
Total:3
3
5
2
Total:10
// .... //not output this "...." more now
Output (for the second page):
3
1
6
6
Total:16
if I set
$show_per_page = 9;
OUTPUT:
1
2
Total:3
3
5
2
Total:10
3
1
6
6
Total:16
Currently I am try to do with the function paging_from_multi_arr but I am stuck on how implement to got the expeted result:
// page to show (1-indexed)
// number of items to show per page
function paging_from_multi_arr($display_array, $page){
Global $show_per_page;
$start = $show_per_page * ($page-1);
$end = $show_per_page * $page;
$i = 0;
foreach($display_array as $main_order=>$section){
$total = 0;
foreach($section as $sub_order=>$value){
if($i >= $end){
break 2; // break out of both loops
}
$total += $value;
if($i >= $start){
echo $value.'<br>';
}
$i++;
}
if($i >= $start){
echo 'Total:'.$total.'<br>';
}
if($i >= $end){
break;
}
}
$total = count($display_array, COUNT_RECURSIVE);
// Total numbers of elements in $display_array array.
// See http://php.net/manual/en/function.count.php
if ($end < $total){
echo "...";
}
}
$show_per_page = 5;
paging_from_multi_arr($A,$_GET["page"]);
Do you have any idea with the function here? Or could give the better algorithm?
thanks

This should give you the output you're looking for:
function flatten_display_array($display_array){
$new_array = array();
$count = 0;
foreach($display_array as $main_order => $section){
$total = 0;
foreach($section as $sub_order => $value){
$new_array[] = array('main' => $main_order,
'sub' => $sub_order,
'value' => $value);
$total += $value;
$count++;
}
// Add section's total to final element in section
$new_array[$count-1]['total'] = $total;
}
return $new_array;
}
function paging_from_multi_array($display_array, $page = 1, $show_per_page = 3){
if(isset($_GET['page']) && is_numeric($_GET['page'])){
// Page number set externally
$page = $_GET['page'];
}
if(isset($_GET['per_page']) && is_numeric($_GET['per_page'])){
// Per page set externally
$show_per_page = $_GET['per_page'];
}
$start = $show_per_page*($page-1);
$end = $show_per_page*$page;
// Convert array to useable format
$new_array = flatten_display_array($display_array);
/* Formatting elements */
$top_string = '....'; // Indicator continues is on previous page
$bottom_string = '....'; // Indicator continues on next page
$br = '<br />'; // Line break
$indent = ' '; // Indent of value row
$count = 0;
$string = '';
for($i = $start; $i < $end; $i++){
// Loop through visible range
$string .= $indent.$new_array[$i]['value'].$br;
if(isset($new_array[$i]['total'])){
$string .= 'Total: '.$new_array[$i]['total'].$br;
}
}
// Check previous page
if($start > 0 && $start < count($new_array) && !isset($new_array[$start-1]['total'])){
// Started mid-way through section
$string = $top_string.$br.$string;
}
// Check next page
if($end-1 < count($new_array) && !isset($new_array[$end-1]['total'])){
// Stopped mid-way through section
$string .= $bottom_string.$br;
}
return $string;
}
To use it, just call the paging_from_multi_array() function:
echo paging_from_multi_array($A);
This way, if the page number or the amount to show per page is not set, it will default to the ones set in the first line of paging_from_multi_array() (currently page 1 and 3 per page).
Also, look at the lines under /* Formatting Elements */ to set the elements for the output (eg: the '...' before and after each segment.

Without the "total" calculation, it should be a simple pagination (by merging the sub-arrays and paginate on the big array). But actually, "total" has nothing to do with the pagination itself. So I recommending let's first forget the "total" for paginating, then insert it in approriate place later.
My idea begins with creating a merge-array like this:
$B = array(
0=>1,
1=>2
0=>3,
1=>5,
2=>2
0=>3,
1=>1,
2=>6,
3=>6
}
And another array to keep track of the beginning of each sub-array of $A in the merge-array $B:
$C = {0, 2, 5}
Then I can do the pagination pretty simply as usual. About the "total", we can use the original array A to calculate it, then insert into the approriate position based on C.
For a quick example, at page 2, max-per-page = 3
From B, I get the sub-array B1 : B(offset = 1, max-per-page=3, from B[3] to B[5])
$B1 = {
1=>5,
2=>2,
0=>3
}
Based on $C={0,2,5}, by a simple "for" loop, we can have $C[1] = 2 < 3 < 5 = 5 = $C[2] < length(B), so at here we know that we will show 2 sub-array (A[1] and a[2]); and we must calculate and return total(A[1]) as well.
That's my idea. I think it would make things easier to keep track.

Related

Pairwise function different calculation

i have this function:
function pairwiseDifference($arry)
{
$n = count($arry) - 1; // you can add the -1 here
$diff = 0;
$result = array();
for ($i = 0; $i < $n; $i++)
{
// absolute difference between
// consecutive numbers
$diff = abs($arry[$i] - $arry[$i + 1]);
echo $diff." ";
array_push($result, $diff); // use array_push() to add new items to an array
}
return $result; // return the result array
}
$arry = array(20,10,10,50);
echo "<br> Percent of commisions are: ";
// this echos 10,0,40 and assigns whatever your function returns
// to the variable $diffArray
$diffArray = pairwiseDifference($arry);
The problem are that im not expecting this output
becouse first number of array (20) is my commission
and the other numbers are my parents commission (10,10,50).
So basically i need to output like this: (0,0,30)
becouse i take 20 of commission,
first parent not take nothing becouse are less of my commission (10)
second parent not take nothing becouse are less of my commission (10)
and only last parent take 30 becouse are greater than my commission (50 - 20 my commission).
Thanks in advance
Since your first element of the array is your commission and the others are the the commissions of parents, and since it seems that you don't want to include your commission in the result array, you can do something like this:
function pairwiseDifference($arry)
{
$n = count($arry);
$diff = 0;
$result = array();
$myComm = $arry[0]; // your commision
for ($i = 1; $i < $n; $i++)
{
$diff = 0; // set $diff to 0 as default
if($myComm < $arry[$i]) // if your commision < parent commision
$diff = $arry[$i] - $myComm;
echo $diff." ";
array_push($result, $diff);
}
return $result;
}
$arry = array(20,10,10,50);
echo "<br> Percent of commisions are: ";
$diffArray = pairwiseDifference($arry);
echo $diffArray[0]; // prints 0
echo $diffArray[1]; // prints 0
echo $diffArray[2]; // prinst 30
To tweak the logic according to your code, there would be only 3 modifications.
Create a $max variable and assign it the value of $arry[0].
Make difference as 0 if current max is greater than current one, else take the difference.
Calculate the new max again using max() function.
Snippet:
<?php
function pairwiseDifference($arry)
{
$n = count($arry) - 1; // you can add the -1 here
$diff = 0;
$result = array();
$max = $arry[0]; // new addition
for ($i = 1; $i <= $n; $i++) // new modification <= n instead of < n
{
// absolute difference between
// consecutive numbers
$diff = $max < $arry[$i] ? $arry[$i] - $max : 0; // new modification
$max = max($max, $arry[$i]); // new modification
echo $diff." ";
array_push($result, $diff); // use array_push() to add new items to an array
}
return $result; // return the result array
}
$arry = array(20,10,10,50);
echo "<br> Percent of commisions are: ";
// this echos 10,0,40 and assigns whatever your function returns
// to the variable $diffArray
$diffArray = pairwiseDifference($arry);

Get numbers next and after a certain number with dynamic max results with PHP

I have a pagination which renders some pages. I have a setting where the user can define how many pages are to be displayed as numbers in the pagination nav. The first and last number are always visible. Now if the user wants to have 3 numbers on the nav and assuming that i am now in the page 7, then it should look like this:
1 ... 6 7 8 ... 12
If the user wants four items then it should look like that:
1 ... 6 7 8 9 ... 12
Up until now i have the following which gives me 3 before and after the current page
$maxLinks = 3;
$currentPageNumber = 7;
$pages = [];
$pages[] = 1;
for($i = max(2, $currentPageNumber - $maxLinks); $i <= min($currentPageNumber + $maxLinks, 12 - 1); $i++) {
$pages[] = $i;
}
$pages[] = 12;
foreach ($pages as $key => $page) {
$newPage = '...';
if (($key === 0) && $pages[1] !== $page + 1) {
array_splice( $pages, 1, 0, $newPage );
}
$itemBeforeLast = count($pages)-2;
if (is_numeric($pages[$itemBeforeLast]) && ($key === $itemBeforeLast) && $pages[$itemBeforeLast + 1] !== $pages[$itemBeforeLast] + 1) {
array_splice( $pages, $itemBeforeLast +1, 0, $newPage );
}
}
This gives me back the following:
But i only want to get 3 or 4 numbers between the dots (this changes based on the value that the user gives in the settings ($maxLinks variable))
Any help is deeply appreciated
Best regards
As you add them to both sides, you need to divide by 2. Also, remove 1 to account for the current page. Then you just need to account for the possibility of having a non-even number of links to the left&right by rounding (down for the left, up for the right).
And end up with:
for($i = max(2, $currentPageNumber - floor(($maxLinks-1)/2));
$i <= min($currentPageNumber + ceil(($maxLinks-1)/2), 12 - 1); $i++)

php - Return array based on range key

I have an array with of enumerating numbers, like:
$pageNumbers = array(1,2,3,4,5,6,7,8,9,10);
Now I have an active page number $currentPage and want to have based on this, before and after 2 elements - a total number of 5.
$currentPage = 2:
Return: array(1,2,3,4,5)
$currentPage = 6
Return: array(4,5,6,7,8)
$currentPage = 10
Return: array(6,7,8,9,10)
Unfortunately, I did not come up with an elegant and simple method to solve this (2x while, 1 big foreach and so on). Maybe you have an idea.
My first idea was:
foreach ($pageNumbers as $page) {
if($page < $currentPage+3 && $page > $currentPage-3) {
array_push(...);
}
}
This will work for the $currentPage = 6, but if $currentPage = 1, it will only return 1,2,3.
Use array_slice and cut out the part of the array you need.
I use an if to see where to slice the array.
if($currentPage < 3){
$arr = array_slice($pageNumbers,0,5);
}elseif($currentPage > count($pageNumbers)-2){
$arr = array_slice($pageNumbers,-5,5);
}else{
$arr = array_slice($pageNumbers,$currentPage-3,5);
}
See working example below.
https://3v4l.org/fpOFQ

Even distribution of PHP arrays across columns

So, I want to distribute evenly lists across 3 columns. The lists cannot be broken up or reordered.
At the moment, I have 5 lists each containing respectively 4, 4, 6, 3 and 3 items.
My initial approach was:
$lists = [4,4,6,3,3];
$columns = 3;
$total_links = 20;
$items_per_column = ceil($total_links/$columns);
$current_column = 1;
$counter = 0;
$lists_by_column = [];
foreach ($lists as $total_items) {
$counter += $total_items;
$lists_by_column[$current_column][] = $total_items;
if ($counter > $current_column*$links_per_column) {
$current_column++;
}
}
Results in:
[
[4],
[4,6],
[3,3]
]
But, I want it to look like this:
[
[4,4],
[6],
[3,3]
]
I want to always have the least possible variation in length between the columns.
Other examples of expected results:
[6,4,4,6] => [[6], [4,4], [6]]
[4,4,4,4,6] => [[4,4], [4,4], [6]]
[10,4,4,3,5] => [[10], [4,4], [3,5]]
[2,2,4,6,4,3,3,3] => [[2,2,4], [6,4], [3,3,3]]
Roughly what you need to do is loop over the number of columns within your foreach(). That will distribute them for you.
$numrows = ceil(count($lists) / $columns);
$thisrow = 1;
foreach ($lists as $total_items) {
if($thisrow < $numrows){
for($i = 1; $i <= $columns; $i++){
$lists_by_column[$i][] = $total_items;
}
}else{
//this is the last row
//find out how many columns need to fit.
//1 column is easy, it goes in the first column
//2 columns is when you'll need to skip the middle one
//3 columns is easy because it's full
}
$thisrow++;
}
This will be an even distribution, from left to right. But you actually want a modified even distribution that will look symmetrical to the eye. So within the foreach loop, you'll need to keep track of 1.) if you're on the last row of three, and 2.) if there are 2 remainders, to have it skip col2 and push to col3 instead. You'll need to set that up to be able to play around with it,...but you're just a couple of logic gates away from the land of milk and honey.
So, I ended up using this code:
$lists = [4,4,6,3,3];
$columns = 3;
$total_links = 20;
$items_per_column = ceil($total_links/$columns);
$current_column = 1;
$lists_by_column = [];
for ($i = 0; $i < count($lists); $i++) {
$total = $lists[$i];
$lists_by_column[$current_column][] = $lists[$i];
//Loop until reaching the end of the column
while ($total < $items_per_column && $i+1 < count($lists)) {
if ($total + $lists[$i+1] > $items_per_column) {
break;
}
$i++;
$total += $lists[$i];
$lists_by_column[$current_column][] = $lists[$i];
}
//When exiting the loop the last time we need another break
if (!isset($lists[$i+1])) {break;}
//If the last item goes onto the next column
if (abs($total - $items_per_column) < abs($total + $lists[$i+1] - $items_per_column)) {
$current_column++;
//If the last item goes onto the current column
} else if ($total + $lists[$i+1] > $items_per_column) {
$i++;
$lists_by_column[$current_column][] = $lists[$i];
$current_column++;
}
}

paging like stackoverflow's

i'm a newbie in php especially on making pagination.
my question is, how to make paging like stackoverflow's pagination?
i mean paging like this :
1 ... 5 6 7 8 9 ... 25
(the first number and the last number is always appear, but in the middle only 5 numbers with the selected page absolutely in the middle)
in php i have tried making paging,
<?php
//Show page links
for($i=1; $i<=$pages; $i++)
{
echo '<li id="'.$i.'">'.$i.'</li>';
}
?>
but it will be shown all of pages like
1 2 3 4 5 6 7 8 9 10 etc
any body have simple logic example to solve this problem?
many thanks :)
This will generate the numbers as per above with current = 7, pages = 25. Replace the numbers with links to get an actual pagination index.
$current = 7;
$pages = 25;
$links = array();
if ($pages > 3) {
// this specifies the range of pages we want to show in the middle
$min = max($current - 2, 2);
$max = min($current + 2, $pages-1);
// we always show the first page
$links[] = "1";
// we're more than one space away from the beginning, so we need a separator
if ($min > 2) {
$links[] = "...";
}
// generate the middle numbers
for ($i=$min; $i<$max+1; $i++) {
$links[] = "$i";
}
// we're more than one space away from the end, so we need a separator
if ($max < $pages-1) {
$links[] = "...";
}
// we always show the last page
$links[] = "$pages";
} else {
// we must special-case three or less, because the above logic won't work
$links = array("1", "2", "3");
}
echo implode(" ", $links);
Output:
1 ... 5 6 7 8 9 ... 25
Below is a snippet from a general pagination class1 I wrote a few years ago. I have edited it to show the relevant parts only.
// cntAround is the number of pages to show before and after the current
function renderNavigation($cntAround = 1) {
$out = '';
$isGap = false; // A "gap" is the pages to skip
$current = // Current page
$cntPages = // Total number of pages
for ($i = 0; $i < $pages; $i++) { // Run through pages
$isGap = false;
// Are we at a gap?
if ($cntAround >= 0 && $i > 0 && $i < $cntPages - 1 && abs($i - $current) > $cntAround) { // If beyond "cntAround" and not first or last.
$isGap = true;
// Skip to next linked item (or last if we've already run past the current page)
$i = ($i < $current ? $current - $cntAround : $cntPages - 1) - 1;
}
$lnk = ($isGap ? '...' : ($i + 1)); // If gap, write ellipsis, else page number
if ($i != $current && !$isGap) { // Do not link gaps and current
$lnk = '' . $lnk . '';
}
$out .= "\t<li>" . $lnk . "</li>\n"; // Wrap in list items
}
return "<ul>\n" . $out . '</ul>'; // Wrap in list
}
Example 1
cntAround = 1, current = 5, cntPages = 9:
[1] ... [4] 5 [6] ... [9]
Example 2
cntAround = 3, current = 5, cntPages = 11:
[1] [2] [3] [4] 5 [6] [7] [8] ... [11]
1) Article is in Danish. Google Translate'd version is here.
Somewhat like this(pseudo-code):
pg = CurrentPageNo
low = 1
high = MAX_PAGES
if (pg-low <=5)
output 1 to pg-1 [with links]
else
output 1..3 [with links]
output "..."
output (pg-3) to (pg-1) [with links]
output pg
if (high - pg <=5)
output pg+1 to high [with links]
else
output (pg+1) to high-3 [with links]
output "..."
output (high-2) to high [with links]
You could use Zend_Paginator to do just that, and learn to use the Zend Framework while you're at it.
Below is php classes link from where you can download php class for pagination.
http://www.phpclasses.org/search.html?words=paging&x=0&y=0&go_search=1
If you (might potentially) have a large number of pages, consider using "logarithmic" page navigation, as described here (sample code included):
How to do page navigation for many, many pages? Logarithmic page navigation
(Note that it'll work just fine for small numbers of pages, too, of course!)

Categories