-1

I want to make a pagination to my pictures from database. Anything i tried doesnt worked, i have no idea what to do. Someone can explain it for me? Watched many tutorial and still cant find a solution (im beginner ye).

<?php
// Connect to MySQL
$pdo = pdo_connect_mysql();
// MySQL query that selects all the images
$stmt = $pdo->query('SELECT * FROM images ORDER BY uploaded_date DESC');
$images = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>


    <div class="content home">
    <h2>Gallery</h2>
    <p>Welcome to the gallery page, you can view the list of images below.</p>

    <div class="images">
        <?php foreach ($images as $image): ?>
        <?php if (file_exists($image['path'])): ?>
        <a href="#">
            <img src="<?=$image['path']?>" alt="<?=$image['description']?>" data-id="<?=$image['id']?>" data-title="<?=$image['title']?>" width="300" height="200">
            <span><?=$image['description']?></span>
        </a>
        <?php endif; ?>
        <?php endforeach; ?>
    </div>


    <div class="container">
  <ul class="pagination">
    <li class="page-item"><a class="page-link" href="#">Previous</a></li>
    <li class="page-item"><a class="page-link" href="#">1</a></li>
    <li class="page-item"><a class="page-link" href="#">2</a></li>
    <li class="page-item"><a class="page-link" href="#">3</a></li>
    <li class="page-item"><a class="page-link" href="#">Next</a></li>
  </ul>
</div>


</div>
mrmiaki
  • 21
  • 3
  • 2
    If you're just getting started with PHP and want to build applications, I'd strongly recommend looking at various [development frameworks](https://www.cloudways.com/blog/best-php-frameworks/) to see if you can find one that fits your style and needs. They come in various flavors from lightweight like [Fat-Free Framework](https://fatfreeframework.com/) to far more comprehensive like [Laravel](http://laravel.com/). These give you concrete examples to work from and guidance on how to write your code and organize your project's files. Pagination is a solved problem. – tadman Jul 31 '20 at 21:22
  • What have you tried and what did go wrong? The code you post now is just a normal page that shows it all – Baracuda078 Jul 31 '20 at 21:56
  • https://stackoverflow.com/questions/3705318/simple-php-pagination-script – xNoJustice Aug 01 '20 at 07:05

2 Answers2

0

Look of LIMIT and OFFSET

SELECT * FROM images ORDER BY uploaded_date DESC LIMIT 5 OFFSET 10

OFFSET value will have the effect of page. OFFSET 0 is page 1 OFFSET 5 is page 2 OFFSET 10 is page 3 etc .....

0

pagination in php

<?php  
    $limit = 3;  //set  Number of entries to show in a page.
    // Look for a GET variable page if not found default is 1.        
    if (isset($_GET["page"])) {    
    $page  = $_GET["page"];    
    }    
    else { $page=1;    
    } 
    //determine the sql LIMIT starting number for the results on the displaying page  
    $page_index = ($page-1) * $limit;      // 0

    $All_Users=mysqli_query($con,"select * from users limit $page_index, $limit");
    while($row=mysqli_fetch_array($All_Users))
    {
        //show  data in table or where you want..
    }
    $all_data=mysqli_query($con,"select count(*) from users");
    $user_count = mysqli_fetch_row($all_data);   //total count 9  
    $total_records = $user_count[0];   //9
    $total_pages = ceil($total_records / $limit);    // 9/3=  3
    if($page >= 2){
        echo "<a href='blog.php?page=".($page-1)."' class='btn customBtn2'>Previous</a>";
      }
    
    if($page<$total_pages) {
        echo "<a href='blog.php?page=".($page+1)."' class='btn customBtn2'>NEXT</a>";   
    }       
?>
Santosh Dangare
  • 685
  • 1
  • 4
  • 15