简体   繁体   中英

Dynamic Previous page link in PHP

I want to create a "Previous Entries" link in PHP. This is not a simple entries list where you fetch a fixed amount of rows from MySQL database. The catch here is: The row entries may vary each time the link gets called.

What is happening actually is I am looking date wise. I am showing the latest date's entries on the front page. There may be multiple rows with same date. I have to show all the entries on the same page. When user clicks on Previous Entry Link, it will fetch the next most recent date and fetch entries with that date and show it on the page and when user clicks on Previous Entry link again, it should show the third most recent date's entries. In this way it should continue. The same thing I also want to do it for Newer Entries.

I am not getting the idea as to how to go ahead with it. Any help will be highly appreciated.

My language is PHP and database is: MySQL

Instead of using date as method of distinguishing your row, use an auto-ID (IDENTITY column). This way, there is no duplication of ID, and then you can just subtract 1 or add 1 to get previous/next entry.

Edited for clarity/possible language barrier.

If the currently selected date is 2013-03-28 then you need to find the closest previous date. There are tons of ways to do this (in order of efficiency):

  • Use mysql's DATE_DIFF method and order by the difference, you'll find the records with the closest date or and can just query for the date itself and do a second query for all records for that date.
  • Use php to loop through dates previous to the current, querying mysql for any records on that day (inefficient)
  • Query for all unique dates in the db and cache that somewhere, and just lookup whichever date comes next.

Once you know that the previous date of records is 2012-11-01 , you can easily query for all records with such a date/timestamp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM