0

I have these cards with data that comes from a database. This is the reason it is in a while loop. Now my problem is that I don't have a clue how to add pagination to this. If some of you guys have any suggestions or answers please let me know.

$start_from = ($page - 1)*$record_per_page;  
        $query = "SELECT organisation_name, organisation_logo, organisation_url FROM `organisations`";
        $result = mysqli_query($conn, $query);
       
        if ($result ->num_rows > 0) {
            echo '<div id="myItems">';
            echo '<div class="row">';
            while ($row = $result-> fetch_assoc()) {
                echo     '
                            <div class="col-md-6">
                                <div class="card rounded-lg mt-3">
                                    <div class="card-body p-0 shadow">
                                        <div class="row">
                                            <div class="col-md-4 p-0">
                                                <img src="../img/';
                                                if ($row['organisation_logo'] == '') {
                                                    echo 'stock/no-image-icon.png"';
                                                } else {
                                                    echo 'uploads/'.$row['organisation_logo'].'"';
                                                }
                echo                            'class="border p-3" style=" width: 150px; height: 150px;">
                                            </div>  
                                            <div class="col-md-8">
                                                <h3 class="card-title mt-3">'.$row["organisation_name"].' <a class="text-dark" href="http://'.$row["organisation_url"].'" target="_blank"><i class="fas fa-external-link-alt" style="font-size: 18px;"></i></a></h3>
                                                <p>Current active surveys: <b>0</b></p>
                                                <a href="organisation.php?organisation='.$row["organisation_name"].'" class="text-dark"><b>View <i class="fas fa-arrow-circle-right"></i></b></a>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>';
             
            }
            echo '</div>';
        } 
        else {
            echo 'nothing here';
        }
Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

1

You need to use LIMIT and OFFSET functionality in your SQL query. Limit determines how many elements you want to show on one site. Offset is point from where you want to get your items. So, for example – if You want 5 items, and you are on page 3, you have to add LIMIT 5 OFFSET 10. You calculate offset with (page_number-1) * limit equation. If you want to count how many pages you have, you must get total pages number, and use something like $pages = ceil($total / $limit). There are a lot of examples on stackoverflow.

I think this one can be very helpful – Simple PHP Pagination script

You also don't need to do all the calculation on your own, there are plany of pagination library for PHP, check look for them.

Hope it helps you a little.

  • Thank you kind sir, My code works now, If she wansn't already i'd marry my youngest daughter to you. So as a alternative I would like to give you 12 of my finest goats. They are top of the line goats and will make for a great pet. I hope you accept my humble offering. Regards Prince Rashied from India. – Ya like jazz Dec 16 '19 at 14:01