Slicing a Javascript array. 
array=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r'];
console.log(array.slice(0,3));
console.log(array.slice(3,6));
console.log(array.slice(6,9));
console.log(array.slice(9,12));

The above outputs:
["a", "b", "c"]
["d", "e", "f"]
["g", "h", "i"]
["j", "k", "l"]


Same output with PHP:
$input = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r');
print_r(array_slice($input,0,3));
print_r(array_slice($input,3,3));
print_r(array_slice($input,6,3));
print_r(array_slice($input,9,3));

//output

Array
(
[0] => a
[1] => b
[2] => c
)
Array
(
[0] => d
[1] => e
[2] => f
)
Array
(
[0] => g
[1] => h
[2] => i
)
Array
(
[0] => j
[1] => k
[2] => l
)




Misc stuff,

js:
array=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r'];

var Page=1;//page to display
var itemsPage=3;//items per page

var totalItems=array.length;
var totalPages=Math.ceil(totalItems/itemsPage);
var a=(Page-1)*itemsPage;
var b=a+itemsPage;
console.log(array.slice(a,b));//items


js:printing links. TESTED OK!
function coolpaging(item_count, limit, curpage, span)
{
let cur_page = Number(curpage);
let page_count = Math.ceil(item_count/limit);
let current_range = [((cur_page-span) < 1 ? 1 : cur_page-span), ((cur_page+span) > page_count ? page_count : cur_page+span)];

// First and Last pages
let first_page = (cur_page > (span+1)) ? '<a href="' + '1' + '">1</a>' + ((cur_page < (span+3) )? ', ' : ' ... ') : '';
let last_page = (cur_page < (page_count-span)) ? (cur_page > (page_count-(span+2)) ? ', ' : ' ... ') + '<a href="'+ page_count + '">'+page_count+'</a>' : '';

// Previous and next page
let previous_page = (cur_page > 1) ? '<a href="'+ (cur_page-1)+'">Previous</a> | ' : '';
let next_page = (cur_page < page_count) ? ' | <a href="'+ (cur_page+1)+'">Next</a>' : '';

let pages = [];

// Display pages that are in range
for (let x=current_range[0];x <= current_range[1]; ++x)
pages.push('<a href="'+ x+'">'+((x == cur_page) ? '<strong>'+x+'</strong>' : x)+'</a>');

if (page_count > 1)
return '<p class="entpagination">'+ previous_page+first_page+ pages.join(",")+last_page+next_page+'</p>';

return '';
}


php:
$array=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s');

$Page=2;//page to display
$itemsPage=3;//items per page

$totalItems=sizeof($array);
$totalPages=ceil($totalItems/$itemsPage);
$a=($Page-1)*$itemsPage;
var_dump(array_slice($array,$a,$itemsPage));//items

//printing links
function paginationLinks($item_count, $limit, $cur_page, $link,$span)
{
$page_count = ceil($item_count/$limit);
$current_range = array(($cur_page-$span < 1 ? 1 : $cur_page-$span), ($cur_page+$span > $page_count ? $page_count : $cur_page+$span));

// First and Last pages
$first_page = $cur_page > ($span+1) ? '<a href="'.sprintf($link, '1').'">1</a>'.($cur_page < ($span+3) ? ', ' : ' ... ') : null;
$last_page = $cur_page < $page_count-$span ? ($cur_page > $page_count-($span+2) ? ', ' : ' ... ').'<a href="'.sprintf($link, $page_count).'">'.$page_count.'</a>' : null;

// Previous and next page
$previous_page = $cur_page > 1 ? '<a href="'.sprintf($link, ($cur_page-1)).'">Previous</a> | ' : null;
$next_page = $cur_page < $page_count ? ' | <a href="'.sprintf($link, ($cur_page+1)).'">Next</a>' : null;

// Display pages that are in range
for ($x=$current_range[0];$x <= $current_range[1]; ++$x)
$pages[] = '<a href="'.sprintf($link, $x).'">'.($x == $cur_page ? '<strong>'.$x.'</strong>' : $x).'</a>';

if ($page_count > 1)
return '<p class="pagination"><strong>Pages:</strong> '.$previous_page.$first_page.implode(', ', $pages).$last_page.$next_page.'</p>';
}
echo paginationLinks(
400,//total amount of item/rows/whatever,
10,//limit of items per page
$_GET['p'],//current page number
'?p=%d',//url
5//items on each side of current page

);

//links function:
//http://css-tricks.com/snippets/php/pagination-function/


paginationLinks function sample output:

Pages: Previous | 1 ... 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 | Next



Comments
Comments are not available for this entry.
2024 By Angel Cool