Find Next and previous mysql records relative to current page
I have photo details page and i need to put next photo and previous photo link. To achieve this, i need to get previous record and next records relative to index or current photo record from mysql photo table. Generally we assume that it can be achieve by add one in current record’s auto increment id and minus one in current record’s auto increment id. But this is not the correct way to get next and previous record as there might be chances that administrator may remove some photo and it directly affect the auto increment id and as a result user redirect to 404 not found page.
There are some other way to get this. One of those is to get all record and compare with current record. But this will increase load of your page. Here i am showing you the optimum way to achieve this.
Find Next record:
“Select min(primaryid) as next from photos where primaryid > ‘$current_id'”
Find Previous record:
“Select max(primaryid) as previous from photos where primaryid < '$current_id'"
Let me know if you find any problem.