Jump to content

Pagination - need simple help please


jirimail

Recommended Posts

Hey guys, hope someone could help me with this.

Im guite new to php. Actully just doing it to customize some script on my server.

 

I need to show only few pages on my site, so i have this script wich it does for me, but only on one side:

 

if ($pages < $page-3) return;

 

but need add to it the other  side as well: ($pages > $page+3)

 

When i tried elseif, that didnt work.. Here is the whole script:

 

 

function page($url) {
	global $page;
	$pages=1;
	for ($starter=0; $this->total>$starter; $starter+=$this->limit) {
		if ($pages != $page)
			echo "<a href=\"".$url.$pages."\" class=\"page\">$pages</a> \n";
		else
			echo "<b>[ $pages ]</b> \n";
	$pages++;
                if ($pages < $page-3) return;
	}
}

 

Anyone could help? Im honestly lost..

 

Thank you

Jiri

 

 

 

Link to comment
Share on other sites

  • 3 weeks later...

I'm not quite sure what you mean.  Could you elaborate a little more?

 

By the looks of it... it seems like you are trying to show a range of pages according to the current page.  For example:

 

If you are on page 5 then you want to show 3 4 [5] 6 7

 

If you are on page 7 then you want to show 5 6 [7] 8 9

 

Am I correct in assuming this?

Link to comment
Share on other sites

Guest JMitchell

do you mean only include count for group of pages surrounding the page?

$mid = $this->limit / 2;
$starter = ($page >= $mid)?($page - $mid):0;
$ender = (($starter + $this->limit)>$this->total)?$this->total:($page + $mid);
for (; $starter <= $ender; $starter++) {

Link to comment
Share on other sites

<?php

// Where $pages is an array which holds the links for each corresponding page number
// Where $currPage is the current page that the user has selected
// Where $pagesLimit is the maximum range of pages available to be selected by the user....i.e., 1 2 [3] 4 5 <- $pagesLimit = 5

// The math you'll have to use to figure out the start page and end page will be more conviluded considering you may enter in an odd number of pages...so you'll have to figure that part out.
function generateLinks($pages, $currPage, $pagesLimit) {
$startPage = $currPage - ($pagesLimit/2);
$endPage = $currPage + ($pagesLimit/2);
for($i=$startPage; $i<$endPage; $i++) {
	if ($i != $currPage) {
		echo "<a href=\"".$pages[$i]['url']."\" class=\"page\">$i</a> \n";
	} else {
		echo "<b>[ $i ]</b> \n";
	}
}
}

?>

Link to comment
Share on other sites

Guest JMitchell

JakeTheSnake, your code will not work when currPage = 0 or currPage = lastPage - pagesLimit as its going to generate invalid pages which you cant go to.

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.