Search code examples
phpmysqlpagination

How to do paging of pictures in PHP


Anyone Please help me in implementing Paging in my project. I have nearly hundred pictures in images folder. When the user clicks the gallery link, the page will be directed to gallery.php, where the pictures should be shown 10 by 10. Please help me i need solution soon.


Solution

  • You could pass a GET parameter through the URL like so:

    http://yourserver.com/gallery.php?p=1
    

    Where variable p represents the page number.

    Then, inside your php script, have it calculate where to start looking inside the database. If you say you need to display 10 by 10, I am assuming 100 pictures per page. So

    $limit = 100;
    $start = $_GET['p'] * $limit - $limit;
    

    Then your SQL query would be to select $limit entries from $start:

    $sql = "SELECT * FROM `yourtphototable` LIMIT {$start},{$limit};";
    

    and then have an html link to the naxt page:

    $nextpage = $_GET['p'] + 1;
    $link = '<a href="http://yourserver.com/gallery.php?p=' . $nextpage . '">Next</a>';