Page 1 of 1

pagination - coonvert from all pages 2 close pages

Posted: Fri Oct 08, 2004 4:53 pm
by vigge89
I'm currently using this code for sections of my site which needs different pages:

Code: Select all

<?php
########## settings for news
$pg['limit'] = 5; // items per page

########## determine pages
$query['count_news'] = mysql_query ("SELECT count(*) FROM `news`");
$pg['total'] = mysql_fetch_row ($query['count_news']); // total number of items
$pg['total'] = $pg['total'][0];

##### retrieve news for current page
$pg['current'] = $args[1];
if (empty ($pg['current'])) $pg['current'] = 1; // no page has been requested
$pg['start'] = $pg['current'] * $pg['limit'] - ($pg['limit']);

##### create links to pages
### previous link
if ($pg['current'] != 1) { // current page is not the first
	$pg['prev_pg'] = $pg['current'] - 1;
	$pg['prev_link'] = "[url2={$fself}{$pg['prev_pg']}/]« previous[/url2]";
} else { $pg['prev_link'] = "« previous"; }
### next link
if (($pg['total'] - ($pg['limit'] * $pg['current'])) > 0) { // there are more items remaining
	$pg['next_pg'] = $pg['current'] + 1;
	$pg['next_link'] = "[url2={$fself}{$pg['next_pg']}/]next »[/url2]";
} else { $pg['next_link'] = "next »"; }
### page links (numbers)
$pg['total_pg'] = $pg['total'] / $pg['limit'];
for($i = 1; $i <= $pg['total_pg']; $i++){ // loop trough each page link
	if ($i == $pg['current']) { $pg['num_links'] .= "{$i} "; }
	else { $pg['num_links'] .= "[url2={$fself}{$i}/]{$i}[/url2] "; };
} // loop trough each page link
if(($pg['total'] % $pg['limit']) != 0) {
	if ($i == $pg['current']) { $pg['num_links'] .= "{$i} "; }
	else { $pg['num_links'] .= "[url2={$fself}{$i}/]{$i}[/url2] "; };
}
######### /determine pages ##########
?>
This could output something like:
« previous | 1 2 3 4 5 6 7 8 9 10 11 12 | next »
However, i've decided to change into another system, which only shows the 3 pages before and after, instead of the old one which just links to all pages available, since the list can get very long.
It should output something like this (if current page is 5):
« previous | 2 3 4 5 6 7 8 | next »
I can't come up with any idea on how to do this, so I decided to ask here. Have anyone done this before, or have you got any links to example code / guides regarding pagination in this way?

Posted: Fri Oct 08, 2004 5:44 pm
by John Cartwright

Code: Select all

<?php

$plus = $current_page + 5;
$minus = $current_page - 10;

if (( $current_page < $plus ) || ($current_page > $minus ))
{

//add page number to list

} 
else
{

//do nothing

}
?>
hope this gives you an idea, i think you were overcomplicating your problem

Posted: Fri Oct 08, 2004 10:36 pm
by timvw
my pagination bar usually looks like:

Image

snippet for building links:

http://www.tonymarston.net/php-mysql/pagination.html#a4 section 7

Posted: Sat Oct 09, 2004 6:39 am
by vigge89
Alright, i've changed it now. It works perfect when I have more than 5 or something pages (tested it with 12). Howeverm i also tried it out on a table which only filled up 2 pages, and it didn't work :S
it just shows a link to the first page (if you are on the second), and if you're on the first, it just shows an 1, no link / text to the second page.

Here's the changed code:

Code: Select all

########## settings for news
$pg['limit'] = 5; // items per page
$pg['close'] = 3; // number of close links (comment to show links to all pages)

########## determine pages
$query['count_news'] = mysql_query ("SELECT count(*) FROM `news`");
$pg['total'] = mysql_fetch_row ($query['count_news']); // total number of items
$pg['total'] = $pg['total'][0];

##### retrieve news for current page
$pg['current'] = $args[1];
if (empty ($pg['current'])) $pg['current'] = 1; // no page has been requested
$pg['start'] = $pg['current'] * $pg['limit'] - ($pg['limit']);

##### create links to pages
### previous link
if ($pg['current'] != 1) { // current page is not the first
	$pg['prev_pg'] = $pg['current'] - 1;
	$pg['prev_link'] = "[url2={$fself}{$pg['prev_pg']}/]« previous[/url2]";
} else { $pg['prev_link'] = "« previous"; }
### next link
if (($pg['total'] - ($pg['limit'] * $pg['current'])) > 0) { // there are more items remaining
	$pg['next_pg'] = $pg['current'] + 1;
	$pg['next_link'] = "[url2={$fself}{$pg['next_pg']}/]next »[/url2]";
} else { $pg['next_link'] = "next »"; }
### page links (numbers)
$pg['total_pg'] = $pg['total'] / $pg['limit'];
for($i = 1; $i <= $pg['total_pg']; $i++){ // loop trough each page link

	if ($i < $pg['current'] + $pg['close'] +1 && $i > $pg['current'] - $pg['close'] -1) { // current link is in bounds to act like a close page
		if ($i == $pg['current']) { $pg['num_links'] .= "{$i} "; }
		else { $pg['num_links'] .= "[url2={$fself}{$i}/]{$i}[/url2] "; };
	} // current link is in bounds to act like a close page

} // loop trough each page link
if($pg['current'] > $pg['total_pg'] - $pg['close'] && ($pg['total'] % $set['limit']) != 0) {
	if ($i == $pg['current']) { $pg['num_links'] .= "{$i} "; }
	else { $pg['num_links'] .= "[url2={$fself}{$i}/]{$i}[/url2] "; };
}
######### /determine pages ##########
any ideas on what's wrong?

Posted: Mon Oct 11, 2004 9:47 am
by vigge89
anyone? can't find it :(

Posted: Mon Oct 11, 2004 1:44 pm
by John Cartwright
What is getting outputted now?

Posted: Mon Oct 11, 2004 3:16 pm
by vigge89
http://vigge.ath.cx/
« previous | 1 | next »
and on the second page:
« previous | 1 | next »
(bold texts are links)

Posted: Thu Oct 14, 2004 9:11 am
by vigge89
umm, noone? :'(

Posted: Thu Oct 14, 2004 9:49 am
by phpScott
does it still work if you go to page 11 of 12 or do you get
8 9 10 11 12 13 14

because if it does you need to do something simillar for the smaller amounts.

Posted: Thu Oct 14, 2004 11:49 am
by vigge89
yup, it works when theres "alot" of pages
i still don't have a clue on why it only displays the 1, and not the 2 :/

Posted: Thu Oct 14, 2004 3:06 pm
by mudkicker
can it be a problem in line 30 with additions ??

Posted: Thu Oct 14, 2004 5:08 pm
by kettle_drum
Check out this class i wrote to do the same task, seems to do what you need:

http://www.macwhore.net/articles/pagination.phps

Posted: Fri Oct 15, 2004 10:47 am
by vigge89
phew, finnaly got it too work now, there were som well-hidden varibales with the wrong names, and also some bad if-statements (which didn't occur). Here's the final code:

Code: Select all

########## settings for entries
$pg['limit'] = 6; // items per page
$pg['close'] = 3; // number of close links (comment to show links to all pages)

########## determine pages
$query['count_entries'] = mysql_query ("SELECT count(*) FROM `entries`");
$pg['total'] = mysql_fetch_row ($query['count_entries']); // total number of items
$pg['total'] = $pg['total'][0];

##### retrieve entries for current page
$pg['current'] = $args[1];
if (empty ($pg['current'])) $pg['current'] = 1; // no page has been requested
$pg['start'] = $pg['current'] * $pg['limit'] - ($pg['limit']);

##### create links to pages
### previous link
if ($pg['current'] != 1) { // current page is not the first
	$pg['prev_pg'] = $pg['current'] - 1;
	$pg['prev_link'] = "[url2={$fself}{$pg['prev_pg']}/]« previous[/url2]";
} else { $pg['prev_link'] = "« previous"; }
### next link
if (($pg['total'] - ($pg['limit'] * $pg['current'])) > 0) { // there are more items remaining
	$pg['next_pg'] = $pg['current'] + 1;
	$pg['next_link'] = "[url2={$fself}{$pg['next_pg']}/]next »[/url2]";
} else { $pg['next_link'] = "next »"; }
### page links (numbers)
$pg['total_pg'] = $pg['total'] / $pg['limit'];
for($i = 1; $i <= $pg['total_pg']; $i++){ // loop trough each page link

	if ($i < $pg['current'] + $pg['close'] +1 && $i > $pg['current'] - $pg['close'] -1) { // current page is in bounds for linking
		if ($i == $pg['current']) { $pg['num_links'] .= "{$i} "; }
		else { $pg['num_links'] .= "[url2={$fself}{$i}/]{$i}[/url2] "; }
	}
} // loop trough each page link
### last incomplete page
if($pg['current'] > $pg['total_pg'] - $pg['close'] && ($pg['total'] % $pg['limit']) != 0) {
	if ($i == $pg['current']) { $pg['num_links'] .= "{$i} "; }
	else { $pg['num_links'] .= "[url2={$fself}{$i}/]{$i}[/url2] "; };
}
######### /determine pages ##########
Thanks for all ther replies, and kettle_drum, your class looked rather nice :)