Jump to content

i need help with pages orders


kfir91

Recommended Posts

i draw the database data to my page and i orders that data in pages

with LIMIT $start and $max ...

this is my for :

	for($i=1; $i <= $pages+1; $i++) {
	if ($i == "".($page+1)."")
		$tp .= "<span class=\"current\">{$i}</span>";
	else
		$tp .= "<a href=\"?act=$thepage&page=".($i-1)."\">{$i}</a>";
}

the for give me list of all the pages that he order.

so now i need way that i order the page more.

to example

i have 7 pages

if i inside 5 page i want that this see pages: 3,4,5,6,7,8

i want two pages before the page and three pages after the page.

if i in 2 page i will see : 1,2,3,4,5,6

if i in 1 page i will see : 1,2,3,4,5,6

if i in 6 page i will see : 2,3,4,5,6,7

...

if u have another order pls say me ...

tnx :)

Link to comment
https://forums.phpfreaks.com/topic/158250-i-need-help-with-pages-orders/
Share on other sites

I think i'd go with something like this:

 

<?php
$currPage = 6;//the page you're on
$pagesToShow = 5;//the number of pages to show
$pages = array($currPage);//an array of the pages we'll show
$previousPages = 0;//the number of pages we've added that are before the current one
$maxPages = 10;//the maximum number of pages available

//Add up to 2 pages before the current one
while($previousPages <2 ){
if($currPage-1 > 0){
	array_unshift($pages,--$currPage);
	$previousPages++;
	$pagesToShow++;
}else{
	break;
}
}

//add the rest of the pages after, if they exist
while($pagesToShow > ){
if($currPage+1 <= $maxPages){
	array_push($pages,++$currPage);
	$pagesToShow++;	
}else{
	break;
}
}

//output the pages

foreach($pages as $page){
echo $page.' ';
}
?>

 

Untested, but you should get the general idea.

it looks like from your code $page is what the current page is, and $pages is what your total page count is.  Your loop is:

 

for($i=1; $i <= $pages+1; $i++) {

 

So instead of counting from 1 to $pages, you would count from ($page - $x) to ($page + $x) where $x is how many pages on each side of the current page you want.  You will also want to add a condition inside the loop to make sure the current iteration ($i) is > 1 and < $pages before you echo something.

it looks like from your code $page is what the current page is, and $pages is what your total page count is.  Your loop is:

 

for($i=1; $i <= $pages+1; $i++) {

 

So instead of counting from 1 to $pages, you would count from ($page - $x) to ($page + $x) where $x is how many pages on each side of the current page you want.  You will also want to add a condition inside the loop to make sure the current iteration ($i) is > 1 and < $pages before you echo something.

 

and if the condition not ok ?

what the else

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.