Jump to content

php pagination


chriscloyd

Recommended Posts

I am trying to do my own pagination script to better learn php i do need some help.. First let me explain my goal, then show you the code I have which is not working.

I have 5 blogs per page;

I have at least 30 blogs in the database for testing purpose;

I want the pagination to go as shown

if you are first viewing the blog no page selected it will show [1] 2 3 4 5 ... Last Page

if you for example choose page three it will show 1 2 [3] 4 5 ... Last Page

Now what is happening when you click on page 5 or 6 it does First Page... 4 5 [6]

When I would like it to show First Page... 2 3 4 5 [6] or 2 3 4 [5] 6

here is my code

<?php
	//if current page != 1 show first page link
	$Show .= ($this->CurPage != 1 ? "<a href=\"?module=blog&page=1\">First Page</a>... " : "");

	//We need the number of start for the for statement
	//We need to check if the number of pages excedes three if it does then make the start minus 2 of the current page
	$Start = (($this->CurPage - 2) >= 1 ? $this->CurPage - 2 : 1);

	//We need the end page for the for statement
	$End = (($this->CurPage + 2) <= $this->NumOfPages ? $this->CurPage + 2 : $this->NumOfPages);

	//Run End Again to make sure
	$NewEnd = ($End <= 3 && $this->NumOfPages > 5 ? $End + 2 : $End);

	//Run Start again to make sure
	$NewStart = ($this->CurPage >= ($this->NumOfPages - 2) ? $Start - 2 : $Start);

	for ($i = $NewStart; $i <= $NewEnd; $i++) {

		$Show .= ($i == $this->CurPage ? "<a href=\"?module=blog&page={$i}\"><strong>[{$i}]</strong></a> " :"<a href=\"?module=blog&page={$i}\">{$i}</a> " );

	}

	//If More than one page and you are not on the last page
	$Show .= ($this->CurPage != $this->NumOfPages && $this->NumOfPages > 1  ? " ...<a href=\"?module=blog&page={$this->NumOfPages}\">Last Page</a>" : "");

	return $Show;
?>

Link to comment
Share on other sites

Chris,

  I'm confused how that could be, since you're using a bunch of constants and arithmetic that explain the behavior you are complaining about. 

 

Let's just focus on this:

 

$Start = (($this->CurPage - 2) >= 1 ? $this->CurPage - 2 : 1);

 

Substitute '5' for $this->CurPage, and it's clear that you set $Start to '3'.

Link to comment
Share on other sites

I guess, I am lost at what you are asking or helping with.  I am trying to always show 5 Results.  When I am on page one it will only show [1] 2 3 when I am on page 6 it will only show 4 5 [6].  So I guess I must just be completely confused or what.

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.