Thread: arrays......
View Single Post
Old 04-17-2009, 02:07 PM   #17 (permalink)
allworknoplay
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Hey guys, I believe I got this part working. It's for my pagination script. The purpose is to show the first 2 and last 2 links with the current page in the middle like so:

current page is: 7

5 6 7 8 9

current page is: 2

1 2 3 4

current page is: 10

8 9 10 11 12


So what I do is use a FOR loop and only take what's in the conditions and put into an array.
Here's the code...


Code:
/* CURRENT PAGE */
$get_page = 13;

/* SIMULATE AVAILABLE PAGES BY USING ARRAY FOR TESTING */
$array = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14);

$count = count($array);

/* WE WANT THE FIRST 2 AND LAST 2 LINKS */
for($i=0;$i<=$count;$i++) {

	if((($get_page - 2) == $array[$i]) || (($get_page - 1) == $array[$i]) || ($get_page == $array[$i]) || (($get_page + 1) == $array[$i]) || (($get_page + 2) == $array[$i])) {
	
	$array2[] = $array[$i];
	}



}
This returns:

11 12 13 14

Since 14 is the last page, there is no page 15....


Comments/criticisms?
allworknoplay is offline  
Reply With Quote