Find Next and previous mysql records relative to current page

mysql-database-freelancer-developer-2

Hello Friends,

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.

About Rakshit Patel
I am PHP Freelancer, Rakshit Patel AKA WordPress Freelancer working as PHP Programmer India. I am here to share all my developing knowledge, quick coding techniques, quick solutions to save your time and help you guys for any issue or bug fixing. If you have any PHP, WordPress, Magento related work, please contact me. I am interested for the work.

2 Comments on Find Next and previous mysql records relative to current page

  • Ankur Gandhi
    October 19, 2010 at 7:08 am

    Nice Share Patel.

  • ask
    February 22, 2015 at 7:53 pm

    What’s up, every time i used to check website posts here in the early hours
    in the daylight, for the reason that i love to gain knowledge of more and
    more.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sponsors

Sponsors

Recommended

DHgate Computers
DHgate.com - Computers, Cell Phones & more
  • We find everbuying built in php and use css to control the style.

About Author

PHP Freelancer

PHP Freelancer I am PHP Freelancer, Rakshit Patel working as PHP Programmer India. I am here to share all my developing knowledge with you people. For Hire PHP Developer India, contact me.
Follow on twitter: @raxit4u2

Get Free Programming Tutorials:

Become a Fan

Popular Topics

Recent Posts

Most Popular

Recent Comments

Get a Free Quote
$
$
$
Security Question: 3 + 8 = ?
$
 
Listing all pages