Here's an idea for you:

Rather than try to pass the uid through the pages, pass a counter that will
act as an index to your result array.  For example, let's say you have a
query like: select * from myTable order by someCol;  The order by will help
give some sort of uniformity to the list on each page.  Each time you access
the page, you would run this query and stuff everything into an array.  The
index would be passed in and indicate which record in the array to display.
You would know whether to display the 'prev' and 'next' labels by comparing
the index to the size of the array.

If your table is large than you can optimize your query so you're not
returning all the rows everytime.  If the index was $idx then you could do
something like: select * from myTable order by someCol limit $idx;  So if
you had a hundred records and your index was 10 then you would only get the
first 10 records.  Since we're including the order by clause, the 10 should
pretty much stay the same if the table doesn't change very often.  With this
technique you would need to do a count() to find out the maximum number rows
in the table.  Since a count() is faster than a query returning many rows,
the combination of these two statements would be a bit faster than running
the original query on a large table.  But then again, on a large table I
doubt someone would want to browse each row.  That's kinda like looking for
a good book by browsing the card catalog at the library... :)

Hope that helps,
Jason Cox
----- Original Message -----
From: "Dan Swensen" <[EMAIL PROTECTED]>
Sent: Tuesday, February 12, 2002 5:30 PM
Subject: [PHP-DB] Flipping through database records

> Hello,
> I am attempting to put together a page whereby results from a database are
> displayed, and previous / next hyperlinks allow the user to "flip" through
> records, one at a time. I attempted to do this by using a unique ID (that
> auto increments)
> and then including links like so at the bottom of the page:
> print("<A HREF=\"profile.php?uid=");
> echo $uid-1;
> print("\">[previous] </A>");
> print("<A HREF=\"profile.php?uid=");
> echo $uid+1;
> print("\">[next]</A>");
> The problem with this, is that if there are gaps in the auto_increment id
> (such as records that have been deleted), the page will (of course) show
> as blank or otherwise muck up. Similarly, once the user moves past the
> record, the same fubar will occur.
> I can't be the first one who has tried to do this... can anyone offer some
> suggestions as to how to go about this more cleanly and efficiently?
> Thanks in advance for any help.
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php

PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to