Jump to content

PHP Pagination (Adjacent)


Rita_Ruah

Recommended Posts

Hello, I have manually created a simple pagination function, but unfortunately, I am having trouble when it comes to limit the number of pages shown... 

At the present, If there are 10 pages, I show all the pages, but my goal was to only show 5 pages... 

Example:

If I am on page 1 I only see:
[1] 2 3 4 5

 

If I am on two:

 

[2] 3 4 5 6

 

If I am on three: 

 

[3] 4 5 6 7

 

What should I do? this is my current cycle:

 

if($number_pages > $num_pages_list /*5 in this case*/)
{
 
for($i=1; $i <= $number_pages; $i++)
{
if($i == $_GET['page'])
{
echo '<a href="'.$page.'?page='.$i.'&i='.($results_page*$i-$results_page).'"><b>'.$i.'</b></a>';
}else{
echo '<a href="'.$page.'?page='.$i.'&i='.($results_page*$i-$results_page).'">'.$i.'</a>';
}
}

 

}
 
In this example I still show every page...
Edited by Rita_Ruah
Link to comment
Share on other sites

for($i=1; $i <= $number_pages; $i++)
Fix that line. If you know what it does, it should be fairly easy to change it to what you've just said you want it to do.

 

Hi there, I know what it does, for i equals 1, while i is lower or equals number_pages, etc etc...

 

But I am not sure what to do here, can you give me a hint? Please, I am stuck here for hours... 

 

Thanks!

Link to comment
Share on other sites

Well, your complaint is that it's printing all the numbers up to $number_pages. You want it to print up to $num_pages_list.

 

You also are starting at 1 right now. You say you want to start at the current page.

 

There are two things to change.

Link to comment
Share on other sites

Well, your complaint is that it's printing all the numbers up to $number_pages. You want it to print up to $num_pages_list.

 

You also are starting at 1 right now. You say you want to start at the current page.

 

There are two things to change.

 

Oh sorry, my bad, ok, I'm going to try to explain again:

 

Currently, my pagination works fine (next - previous - first page - last page), but the problem is that I need to only show 5 numbers (five pages at once, and currently I am showing every page).

 

What I want is showing every page buuuuut make sure that there are only 5 pages at once, thus the example (imagine that there is a total of 7 pages in the pagination:

 

If I am at page 1 I will see:

 

[1] 2 3 4 5

 

If I am at page 2 I will see:

 

[2] 3 4 5 6

 

If I am at page 3:

 

[3] 4 5 6 7

 

If I am at page 4:

 

3 [4] 5 6 7

 

Did you understand? (Sorry I am italian)

Link to comment
Share on other sites

Yes, I understand. I told you what your code is doing now. Change those parts to do what you want.

 

The first part I think that it's assigning the current page for $i, right?

Next I have no idea... Please help... I feel dizzy, pagiantion was fine and was my favorite project until this problem :( 

I only ask because I am desperate... :(

Link to comment
Share on other sites

You can't be that desperate, you didn't even try to change it.

 

But I am on my tablet, is there any php editor/run for tablet (android)?

 

Only tomorrow I will be able to try to fix it... please Jessica, if you don't want to give the answer, atleast try to explain to me what to do... I am thinking about:

 

for($i = $_GET['page']; $i <= $num_pages_list+$_GET['page'];i++)

{

if($i <= $num_pages)

{

echo $i;

}

}

 

EDIT: This won't work because the pages aren't limited to 5 right? 

Damn... :(

Edited by Rita_Ruah
Link to comment
Share on other sites

Viewing page: 1 of 10

[ *1* 2 3 4 5 ]

 

Viewing page: 2 of 10

[ *2* 3 4 5 6 ]

 

Viewing page: 3 of 10

[ *3* 4 5 6 7 ]

 

Viewing page: 4 of 10

[ *4* 5 6 7 8 ]

 

Viewing page: 5 of 10

[ *5* 6 7 8 9 ]

 

Viewing page: 6 of 10

[ *6* 7 8 9 10 ]

 

Viewing page: 7 of 10

[ 6 *7* 8 9 10 ]

 

Viewing page: 8 of 10

[ 6 7 *8* 9 10 ]

 

Viewing page: 9 of 10

[ 6 7 8 *9* 10 ]

 

Viewing page: 10 of 10

[ 6 7 8 9 *10* ]

<?php

$display_pages = 5;
$total_pages = 10;


for($i=1; $i<=10; $i++){
	$current_page = $i;
	echo "Viewing page: {$current_page} of {$total_pages}\n";
	
	echo '[';
	$start = $current_page;
	if($total_pages-$start < $display_pages){
		$start = ($total_pages-$display_pages)+1;
	}
	for($k=$start; $k<($start+$display_pages); $k++){
		if($k==$current_page){
			echo " *{$k}* ";
		}else{
			echo " {$k} ";
		}
	}
	echo ']';
	echo "\n\n";
}
Link to comment
Share on other sites

<?php

$display_pages = 5;
$total_pages = 10;


for($i=1; $i<=10; $i++){
	$current_page = $i;
	echo "Viewing page: {$current_page} of {$total_pages}\n";
	
	echo '[';
	$start = $current_page;
	if($total_pages-$start < $display_pages){
		$start = ($total_pages-$display_pages)+1;
	}
	for($k=$start; $k<($start+$display_pages); $k++){
		if($k==$current_page){
			echo " *{$k}* ";
		}else{
			echo " {$k} ";
		}
	}
	echo ']';
	echo "\n\n";
}

This looks good thanks :)

Whenever I finish the function to paginate (it's a pagination without database), I will tell if it worked! Thank you Jessica!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.