Jump to content

Making Pagination


Vaske

Recommended Posts

Hi I'm fairly new to this and want to make a pagination for my project, the problem is no matter how many times I try I cant seem to make it work without altering the code I already have. I would appreciate if someone could help me figure out How to make a pagination without altering the already existing code I have.

 

Here is the page I want pagination on:

 

<div class="content">
    <div class="row">
        
    
    <?php 

    $query = "SELECT * FROM movie";
    $run = mysqli_query($con,$query);
    if ($run) {

        while($row = mysqli_fetch_assoc($run)){
            ?>

<div class="col">
    <div class="card border border-success" style="width: 18rem;text-align: center; background-color:#343a40;">
  <img class="card-img-top" height="300px" width="100px" src="admin12312/thumb/<?php echo$row['imgpath']; ?>" alt="Card image cap">
  <div class="card-body">
    <h5 class="card-title" style="color: green;"><?php echo$row['name']; ?></h5>
<br>
    
    <a href="viewmovie.php?id=<?php echo $row['id']; ?>" class="btn btn-outline-success my-2 my-sm-0">Watch</a>
  </div>
</div>
</div>

            <?php

        }

    }

     ?>
</div>
</div>

 

Link to comment
Share on other sites

You can't make it work without altering the code you already have. Your code doesn't take pagination into account, so in order to use pagination you must alter your code.

You'll need to add a LIMIT clause to your query, then add links that target the same page with an updated starting record ID. When your page is loaded, check to see if that start number is set - if it is, it becomes the new starting offset in your SQL limit clause. If it doesn't exist, the starting offset is 0.

That should be enough to put you on the right path - give it a shot and if it doesn't work post your code here with your questions.

Link to comment
Share on other sites

3 minutes ago, maxxd said:

You can't make it work without altering the code you already have. Your code doesn't take pagination into account, so in order to use pagination you must alter your code.

You'll need to add a LIMIT clause to your query, then add links that target the same page with an updated starting record ID. When your page is loaded, check to see if that start number is set - if it is, it becomes the new starting offset in your SQL limit clause. If it doesn't exist, the starting offset is 0.

That should be enough to put you on the right path - give it a shot and if it doesn't work post your code here with your questions.

What I meant to say was I just want to keep the card and have pagination with the cards instead of having a pagination with lists. But every tutorial or guide I find makes php and mysql paginations only with lists

Link to comment
Share on other sites

pagination involves two sql queries. the first one gets the total number of matching rows (including any join, where, or having terms), so that you can calculate the total number of pages, used when looping to produce pagination links and to test/limit the requested page number. the second one adds a limit term to the base query to get the requested page of data.

it doesn't matter what your presentation code is doing with the data that it loops over. you are just producing some output for each pass through the loop.

you should actually remove the database specific code from the html document, put it above the start of the html document, then fetch the data that the query matches into an appropriately named php variable. you would then test/loop over this variable where the database specific code is currently at in your html document.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.