First a quick description of what I'm trying to do: User clicks on a link to see one row from a database. User then may click on links for the 'previous' or 'next' entry in the database. I'm using this code for the queries:
$result=mysql_query("SELECT * FROM $table WHERE date>=$post_date ORDER BY date ASC LIMIT 1");
$result=mysql_query("SELECT * FROM $table WHERE date<=$post_date ORDER BY date DESC LIMIT 1");
So far, I have it all working just as I wish except for one detail: how do I identify when either the first or last entry from the database is being viewed so that I can turn off the 'previous' or 'next' links?
I may be missing your point here, but doesn't that only work after the user clicks on the 'previous' or 'next' link? I'd like to know when the last row in the database has been queried so that I can remove the appropriate link and prevent the user from clicking a link that won't return any results.
1. Get the number of rows in the DB
2. Keep track of the current "row index" (this won't be the primary key in most cases)
3. if row_index==0, first roww, don't show preb
if row_index==num_rows, last row, don't show next.
nielsene, keep in mind that the newbie in me may not have the lingo down properly, but here goes...
I tried to use the prev() and next() functions for arrays, but I could never get it to work. My problem there is probably that I don't fully understand how to deal with the query as an array.
I also though about basing it on the unique ID, auto-incrementing value in each row. I didn't take this too far since the order of the entries has to be by date and this particular database allows for post dated entries.
After a bit more browsing for a solution, I'm wondering if I can somehow use the max() and min() functions to look for the earliest and most recent dates (all unix timestamps) and use these values to test if an entry is the first or last. Any thoughts?
Sure you could do a "SELECT MAX(postdate), MIN(postdate) FROM table;";
Then compare if the current entry's date matches one of those. (You'd still need two queries per page, but its slightly better than the 3....