|
Posted by Chuck Anderson on 06/05/05 22:43
Tony Marston wrote:
>Take a look at http://www.tonymarston.co.uk/php-mysql/pagination.html for a
>description of pagination and how to do it.
>
>
>
Thanks for that clear demonstration. I have constructed several of these
sort of mechanisms and use the same basic method, but I think my scripts
are a bit more convoluted. I like your method.
To add to that, I often provide a link from every entry (every row) on
these "index" pages that opens a new page displaying that one entry in
it's entirety. I also include Next and Prev links on that page to the
next and previous individual entries, so the user does not have to go
back to the index page to get to the next and previous individual entries.
[This could be a page that allows modification of individual database
entries, or in the case of a photo gallery it merely displays the
individual photo (displaying n thumbnails per index (selection) page,
and clicking on a thumbnail opens a new window displaying the full size
version of that photo - with next previous links to the next and
previous single photo).]
I hope I can explain the rest of this in a comprehensible manner.
Although I have found a solution that does what I want, I am wondering
if it is really the only way.
The problematic requirement is that I want the next and previous links
on the second page (with the individual entry) to point to the next and
previous entries in the same order as they are on the index page.
If I order my select queries (index page and secondary page) using a
field where all entries are unique (like an index that is auto
incremented), I have no problem using the following queries for previous
and next on the second page (with the individual entries):
(dbase table = packages; - table index, pkg_id is auto incremented; this
entry's index is $pkg_id)
//Is there a previous package?
$prevresult = mysql_query("SELECT * FROM packages WHERE
pkg_id < '$pkg_id'
ORDER BY pkg_id DESC LIMIT 1", $link);
if ($prevrow = @mysql_fetch_array($prevresult)) {
$prevpkg_id=$prevrow[pkg_id];
} else {
$prevpkg_id=0; // signifies no entry
}
//Is there a next package?
$nextresult = mysql_query("SELECT * FROM packages WHERE
pkg_id > '$pkg_id'
ORDER BY pkg_id LIMIT 1", $link);
if ($nextrow = @mysql_fetch_array($nextresult)) {
$nextpkg_id=$nextrow[pkg_id];
} else {
$nextpkg_id=0; // signifies no entry
}
But, if I order the entries on the index page using a field that is not
unique (in this case - pkg_date, where many entries can have the same
pkg_date), I can not find a way to obtain the next and previous entries
(on the second page) in the same order as they appear on the index page.
I have tried the following, but it does not work:
//Is there a previous package?
$prevresult = mysql_query("SELECT * FROM packages WHERE
pkg_date <= '$row[pkg_date]' AND
pkg_id != $pkg_id
ORDER BY pkg_date, DESC LIMIT 1", $link);
if ($prevrow = @mysql_fetch_array($prevresult)) {
$prevpkg_id=$prevrow[pkg_id];
} else {
$prevpkg_id=0;
}
//Is there a next package?
$nextresult = mysql_query("SELECT * FROM packages WHERE
pkg_date >= '$row[pkg_date]' AND
pkg_id != $pkg_id
ORDER BY pkg_date ASC LIMIT 1", $link);
if ($nextrow = @mysql_fetch_array($nextresult)) {
$nextpkg_id=$nextrow[pkg_id];
} else {
$nextpkg_id=0;
}
I think I see the inherent flaw in this (although I'm at my limit of
understanding/confusion).
What I end up doing is keeping track of the row number of the entries on
my index page and passing that as an extra variable to the second page
(where the entry is displayed). Then I can use the same query as I use
on the index page, but limit the result to the next row number and the
prev row number. What I don't like about that solution is that it I have
another variable (row_number) I need to keep passing up and down the
chain (should I go on to other pages).
Is that simply the only way to do it, or is there another way to do what
I want without having to pass the result's row number to keep track?
--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Navigation:
[Reply to this message]
|