Lodius2000 Posted July 18, 2008 Share Posted July 18, 2008 this is straight out of php cookbook recipe 10.13 it makes pagination for a (for example) 13 row table that looks like <<prev | 1-5 | 6-10 | 11-13 | next>> on the page that displays rows 6-10 <<prev | 1-5 | 6-10 | 11-13 | next>> and on the last page: 11-13 <<prev | 1-5 | 6-10 | 11-13 | next>> relevant index.php code looks like this: <?php require 'pagination.php'; //uses pear db but the object names do what you would think // ie, numrows counts the rows, etc $offset = isset($_GET['offset']) ? intval($_GET['offset']) : 1; if (! $offset){ $offset = 1;} $per_page = 5; $q = $db->query("SELECT article_id FROM text_cms"); $total = $q->numrows(); $list = $db->getAll('SELECT * FROM text_cms ORDER BY article_id DESC ' . "LIMIT $per_page OFFSET " . ($offset-1)); $lastRowNumber = $offset - 1; //display contents of list in a foreach loop that contains $lastRowNumber++; //now for the pagination pc_indexed_links($total,$offset,$per_page); print "<br />"; print "(Displaying $offset - $lastRowNumber of $total)"; ?> pagination.php contains the code for pc_indexed_links <?php function pc_print_link($inactive,$text,$offset=''){ if ($inactive){ print "<span class='inactive'>$text</span>"; } else { print "<span class='active'>". "<a href='" . htmlentities($_SERVER['PHP_SELF']) . "?offset=$offset'>$text</a></span>"; } } function pc_indexed_links($total,$offset,$per_page){ $separator = ' | '; //print "<<prev" link pc_print_link($offset == 1, '<< Prev', $offset - $per_page); //print all groupings except last one for ($start = 1, $end = $per_page; $end < $total; $start += $per_page, $end += $per_page){ print $separator; pc_print_link($offset == $start, "$start-$end", $start); } /* print the last grouping - * at this point, $start points to the element at the beginning * of the last grouping */ /* the text should only contain a range if there is more than * one element on the last page. For example, the last grouping * of 11 elements with 5 elements per page should just say 11, not 11-11 */ $end = ($total > $start) ? "-$total" : ''; print $separator; pc_print_link($offset == $start, "$start$end", $start); //print "Next>>" link print $separator; pc_print_link($offset == $start, 'Next >>', $offset + $per_page); } ?> I want to have only prev and next links, I can do this by cutting out the code that prints all the groupings, including the last 1, but then the prev and next links lose the "inactive" class when there are no more entries in that direction, so if i have 20 rows, after i get to the page that contains rows 16-20, 'next' is still clickable and will take me to the nonexistent page that contains rows 21-25 so my question is how do i modify pcindexedlinks so that the 'inactive' class still works but there are only prev and next links thanks for the help, i been tearing my brain apart trying to figure this one out Link to comment https://forums.phpfreaks.com/topic/115333-help-reworking-a-pagination-script/ Share on other sites More sharing options...
Lodius2000 Posted July 18, 2008 Author Share Posted July 18, 2008 b-to the-ump Link to comment https://forums.phpfreaks.com/topic/115333-help-reworking-a-pagination-script/#findComment-593869 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.