Forum Moderators: coopster

Message Too Old, No Replies

multiple page issue

         

FiRe

4:01 pm on Jan 30, 2007 (gmt 0)

10+ Year Member



I have a large database and it splits the results up on different pages...

// Do some page calculation stuff
$page = $_GET['page'];
if ($page <= 0) { $page = 1; }
$perpage = 20; //how many results per page
$pos = $page * $perpage - $perpage;
if ($pos < 0) { $pos = 0; }
$query = mysql_query("SELECT * FROM questions WHERE cat='$catname'");
$pages = mysql_num_rows($query);
if ($pages > 0) {
$pages = $pages / $perpage;
if (strrpos($pages,'.') > 0) {
$pages = substr($pages, 0, strrpos($pages,'.'));
$pages = $pages + 1;
}

$query = mysql_query("SELECT * FROM questions WHERE cat='$catname' ORDER BY id DESC LIMIT $pos, $perpage");
while($r = mysql_fetch_array($query)) {
// Do stuff
}

$pgz = array();
for ($x = 1; $x <= $pages; $x++) {
if ($page == $x) {
$pgz[] = $x;
}
else {
$pgz[] = '<a href="?page='.$x.'">'.$x.'</a>';
}
}
$pages = implode(" ", $pgz);
}
echo $pages;


The problem is if there is over 20 pages, the list of pages at the bottom gets very long! How can I convert this:

Page: 1 2 3 4 5 6 7 8 9 10

Into this:

Page: 1 2 3 4 5 ... 10

And obviously that would change depending on what page you were on. Can anyone help me with my code?
Thanks!

[edited by: FiRe at 4:01 pm (utc) on Jan. 30, 2007]

FiRe

4:35 pm on Jan 30, 2007 (gmt 0)

10+ Year Member



Well here is what I have come up, but its not working correctly:

if ($pages > 5) {

if ($page <= $pages-3) {
$output = array_slice($pgz, $page-1, 3);
$output2 = array_slice($pgz, $pages-3, 3);

$x = (($pages-$page) <= 5)? ' ' : ' .. ';

$pages = implode(" ", $output) . $x . implode(" ", $output2);
}
else {
$output = array_slice($pgz, $pages-5, 5);
$pages = '<a href="1">1</a> .. '.implode(" ", $output);
}

}
else {
$pages = implode(" ", $pgz);
}
}

Say there are 11 pages:

PAGE 5 (OK)
5 6 7 .. 9 10 11

PAGE 6 (OK)
6 7 8 9 10 11

PAGE 7 (BAD)
7 8 9 9 10 11

PAGE 8 (BA)
8 9 10 9 10 11

PAGE 9 (OK)
1 .. 7 8 9 10 11

Any ideas?

cameraman

6:15 pm on Jan 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just change this:
if ($page <= $pages-3) {

to this:
if ($page <= $pages-5) {

That's some slick code - I'll have to add it to my library :)