pagination - coonvert from all pages 2 close pages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

pagination - coonvert from all pages 2 close pages

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

my pagination bar usually looks like:

Image

snippet for building links:

http://www.tonymarston.net/php-mysql/pagination.html#a4 section 7
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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?
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

anyone? can't find it :(
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

What is getting outputted now?
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

http://vigge.ath.cx/
« previous | 1 | next »
and on the second page:
« previous | 1 | next »
(bold texts are links)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

umm, noone? :'(
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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 :/
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

can it be a problem in line 30 with additions ??
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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 :)
Post Reply