Jump to content

Recommended Posts

i have a pagination script working fine but the numbers at the bottom do not indicate what page i am currently on, how can change this to show that i am currently on page 5 for example by making the number 5 bold or underlined

 

<?php
for ($i=1; $i<=$maxPages; $i++)
   echo ($i === $page) ? $i .' ' : '<a href="?page='. $i . (strlen($search) ? '&search=' . $search : '') .'" class="pagenumbers">'. $i .'</a> ';
?>

Link to comment
https://forums.phpfreaks.com/topic/173664-solved-page-numbers/
Share on other sites

Actually it is indicating it already now, it is not printing the current page as link now. But the code below would change it not a link and bolded.

 

echo ($i === $page) ? '<strong>' . $i .'</strong> ' : '<a href="?page='. $i . (strlen($search) ? '&search=' . $search : '') .'" class="pagenumbers">'. $i .'</a> ';

Link to comment
https://forums.phpfreaks.com/topic/173664-solved-page-numbers/#findComment-915417
Share on other sites

Changing that piece of code should have not affected anything else but bolding the page number if its current. I tested it and for me it prints all pages no matter how many there are. This is the code I tested and it works.

 

<?php
// Print pages
for ($i=1; $i<=$maxPages; $i++)
{
echo ($i === $page) ? '<strong>' . $i .'</strong> ' : '<a href="?page='. $i . (strlen($search) ? '&search=' . $search : '') .'">'. $i .'</a> ';
}

Link to comment
https://forums.phpfreaks.com/topic/173664-solved-page-numbers/#findComment-915465
Share on other sites

sorry ill explain it a bit better, the page numbers below the results are infinate depending on the amount of results and the results per page, the page numbers can go above ten but i do not think that is user friendly... i tried this but on the 10th page the option to click next is not linked and will not allow you to view page 11...

 

$maxPages = ceil($foundnum / $perPage); //may come out as a decimal 
if ($maxPages > 10)
$maxPages = 10;

Link to comment
https://forums.phpfreaks.com/topic/173664-solved-page-numbers/#findComment-915483
Share on other sites

You probably should code the paging like that it would print out something like this, say you are on the page 10 and the print will be: prev ... 8 9 10 11 12 ... next. And make it dynamic. But I don't have the time to do everything for you ready. Maybe someone else has. There is still plenty of tutorials that show how to do this if you search on google. And I think I had a paging class somewhere if you want I can find and post it here. It had this functionality and some others also.

 

Edit: but I think the class might not help but instead make it more complicated. So it might be a good idea to try to understand and learn the current code and try modifying it. Or follow some tutorial.

Link to comment
https://forums.phpfreaks.com/topic/173664-solved-page-numbers/#findComment-915508
Share on other sites

First of all a WARNING, this class was created once for a specific app and thats why it might not be optimal to all apps. Also this might break a little concepts of OOP but here goes.

 

<?php
/*
* 
* Pagination class.
*
*/

class PagerException extends Exception {}

class Pager {

// vars
private $currentPage;
private $totalPages;
private $url; // Extra GET parameters before the paging parameter.
private $urlEnd; // (optional) if there is need for an addon to the url after paging stuff ie. searches, sorting, etc.
private $extra; // Some other extra to the href attribute eg. added javascript.
private $firstPageButton;
private $lastPageButton;
private $nextButton;
private $previousButton;
private $interval;
private $maxInterval;
private $pagePrefix;
private $pageSuffix;

// Constructor
function __construct($totalPages, $url = '', $currentPage = 1, $urlEnd = null, $extra = null, $beginWithQuestionMark = false)
{	
	if ($currentPage > $totalPages)
	{
		throw new PagerException('Current page can not be larger than total pages.');
	}
	else
	{
		$this->currentPage = $currentPage;
	}
	$this->totalPages = $totalPages;
	$this->firstPageButton = '«';
	$this->lastPageButton = '»';
	$this->previousButton = '‹';
	$this->nextButton = '›';
	if (!strlen($url))
	{
		$this->url = '?p=';
	}
	else if ($beginWithQuestionMark)
	{
		$this->url = '?p=';
	}
	else
	{
		$this->url = $url .'&p=';
	}
	$this->maxInterval = intval(floor(($this->totalPages/2)))-1;
	$this->interval = 2;
	$this->pagePrefix = '';
	$this->pageSuffix = '';

	if (!is_null($urlEnd))
	{
		$this->urlEnd = $urlEnd;
	}

	if (!is_null($extra))
	{
		$this->extra = $extra;
	}
}

// Initialize pager.
function init()
{
	$output = '';
	// First page button.
	if ($this->currentPage == 1)
	{	
		$output .= $this->firstPageButton .' ';
	}
	else
	{
		$output .= '<a href="'.$this->url.'1'. $this->urlEnd .'" '. $this->extra .'>'.$this->firstPageButton.'</a> ';
	}

	// Previous page button.
	if ($this->currentPage == 1)
	{	
		$output .= $this->previousButton .' ';
	}
	else
	{
		$output .= '<a href="'.$this->url.($this->currentPage-1) . $this->urlEnd .'" '. $this->extra .'>'.$this->previousButton.'</a> ';
	}		


	// When there is very few pages.
	if($this->totalPages <= (2*$this->interval+1))
	{
		$i = 1;
		while ($i <= $this->totalPages)
		{
			if ($i == $this->currentPage)
			{
				$output .= $this->pagePrefix.$this->currentPage.$this->pageSuffix.' ';
			}
			else
			{
				$output .= '<a href="'.$this->url.$i . $this->urlEnd .'" '. $this->extra .'>'.$this->pagePrefix.$i.$this->pageSuffix.'</a> ';
			}
			$i++;
		}
	}

	// when current_page <= interval + 1
	else if ($this->currentPage <= $this->interval+1)
	{
		$i = 1;
		while($i < $this->currentPage)
		{
			$output .= '<a href="'.$this->url.$i . $this->urlEnd .'" '. $this->extra .'>'.$this->pagePrefix.$i.$this->pageSuffix.'</a> ';
			$i++;
		}
		$output .= $this->pagePrefix.$this->currentPage.$this->pageSuffix.' ';
		$i = $this->currentPage+1;
		while ($i <= $this->currentPage + $this->interval)
		{
			$output .= '<a href="'.$this->url . $i . $this->urlEnd .'" '. $this->extra .'>'.$this->pagePrefix.$i.$this->pageSuffix.'</a> ';
			$i++;
		}
		$output .= '... ';
	}

	// when current_page >= total_pages - interval
	else if ($this->currentPage >= ($this->totalPages - $this->interval))
	{
		$output .= '... ';
		$i = $this->currentPage - $this->interval;
		while($i < $this->currentPage)
		{
			$output .= '<a href="'.$this->url . $i . $this->urlEnd .'" '. $this->extra .'>'.$this->pagePrefix.$i.$this->pageSuffix.'</a> ';
			$i++;
		}
		$output .= $this->pagePrefix.$this->currentPage.$this->pageSuffix.' ';
		$i = $this->currentPage+1;
		while ($i <= $this->totalPages)
		{
		$output .= '<a href="'.$this->url . $i . $this->urlEnd .'" '. $this->extra .'>'.$this->pagePrefix.$i.$this->pageSuffix.'</a> ';
			$i++;
		}
	}

	// when current_page is somewhere between and there is more pages than 2*interval+currentpage.
	else
	{
		$output .= '... ';
		$i = $this->currentPage - $this->interval;
		while ($i < $this->currentPage)
		{
		$output .= '<a href="'.$this->url.$i . $this->urlEnd . '" '. $this->extra .'>'.$this->pagePrefix.$i.$this->pageSuffix.'</a> ';
			$i++;
		}
		$output .= $this->pagePrefix.$this->currentPage.$this->pageSuffix.' ';
		$i = $this->currentPage+1;
		while ($i <= $this->currentPage+$this->interval)
		{
		$output .= '<a href="'.$this->url . $i . $this->urlEnd .'" '. $this->extra .'>'.$this->pagePrefix.$i.$this->pageSuffix.'</a> ';
			$i++;
		}
		$output .= '... ';
	}

	// Next page button.
	if ($this->currentPage == $this->totalPages)
	{
		$output .= $this->nextButton .' ';
	}
	else
	{
		$output .= '<a href="'.$this->url . ($this->currentPage+1) . $this->urlEnd .'" '. $this->extra .'>'.$this->nextButton.'</a> ';
	}

	// Last page button
	if ($this->currentPage == $this->totalPages)
	{
		$output .= $this->lastPageButton .' ';
	}
	else
	{
		$output .= '<a href="'.$this->url . $this->totalPages . $this->urlEnd .'" '. $this->extra .'>'.$this->lastPageButton.'</a> ';
	}
	return $output;
}

// Get current page.
function getCurrentPage()
{
	return $this->currentPage;
}

// Sets the current page.
function setCurrentPage($page)
{
	if ($page < 1 || $page > $this->totalPages)
	{
		throw new PagerException('Invalid page number.');
	}
	else
	{
		$this->currentPage = $page;
	}
}

// To set customized first page button.
function setFirstPageButton($code)
{
	$this->firstPageButton = $code;
}

// To set customized last page button.
function setLastPageButton($code)
{
	$this->lastPageButton = $code;
}

// To set customized previous page button.
function setPreviousButton($code)
{
	$this->previousButton = $code;
}

// To set customized next page button.
function setNextButton($code)
{
	$this->nextButton = $code;
}

// To set customized page prefix.
function setPagePrefix($prefix)
{
	$this->pagePrefix = $prefix;
}

// To set customized page suffix.
function setPageSuffix($suffix)
{
	$this->pageSuffix = $suffix;
}

// To set how many next/previous page links we will show around current page.
function setInterval($interval)
{
	if ($interval < 1 || $interval > $this->maxInterval)
	{
		throw new PagerException('Invalid interval number.');
	}
	else
	{
		$this->interval = $interval;
	}
}
}

 

And usage for example

<?php
$currentPage = 10;
$maxPages = 20;

$paging = new Pager($maxPages, null, $currentPage, null, null, true);
echo $paging->init();

 

Of course you calculate the maxpages and currentpage etc. based on your queries. And might modify the url if needed. Plus some other stuff which is not so well commented.

Link to comment
https://forums.phpfreaks.com/topic/173664-solved-page-numbers/#findComment-915518
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.