Vaske Posted November 14, 2021 Share Posted November 14, 2021 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> Quote Link to comment https://forums.phpfreaks.com/topic/314231-making-pagination/ Share on other sites More sharing options...
maxxd Posted November 14, 2021 Share Posted November 14, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/314231-making-pagination/#findComment-1592081 Share on other sites More sharing options...
Vaske Posted November 14, 2021 Author Share Posted November 14, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/314231-making-pagination/#findComment-1592082 Share on other sites More sharing options...
mac_gyver Posted November 14, 2021 Share Posted November 14, 2021 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314231-making-pagination/#findComment-1592085 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.