Automatic Page Scroll Load Data

In this tutorial we will show you how to create a Automatic Page Scroll Load Data in PHP. Automatic Page Scroll Load Data will help us and show an automatic process to load more data from the database in PHP. Creating a Automatic Page Scroll Load Data compose of a script using jQuery, Ajax, and PHP. And it will be very user friendly because of the data that would be loaded from the MySQL database automatically while you scrolling the page down. I have a example script of code.

Sample Code

Data.php - Using the data.php file, we’ll fetch the data from the MySQL database based on the last data ID and generate the data list HTML. Ajax success method gets this HTML and renders the HTML content to the respective DIV element.
  1. <?php
  2. if(isset($_POST["id"]) && !empty($_POST["id"])){
  3. include('dbConfig.php');
  4.         $lastID = $_POST['id'];
  5.         $showLimit = 1;
  6.         $queryAll = $db->query("SELECT COUNT(*) as num_rows FROM data WHERE id < ".$lastID." ORDER BY id DESC");
  7.         $rowAll = $queryAll->fetch_assoc();
  8.         $allNumRows = $rowAll['num_rows'];
  9.         $query = $db->query("SELECT * FROM data WHERE id < ".$lastID." ORDER BY id DESC LIMIT ".$showLimit);
  10. if($query->num_rows > 0){
  11.     while($row = $query->fetch_assoc()){
  12.         $postID = $row["id"]; ?>
  13.         <div class="list-item"><a href="javascript:void(0);"><h2><?php echo $row["title"]; ?></h2></a></div>
  14. <?php } ?>
  15. <?php if($allNumRows > $showLimit){ ?>
  16.     <div class="load-more" lastID="<?php echo $postID; ?>" style="display: none;">
  17.         <img src="assets/loading.gif"/>
  18.     </div>
  19. <?php }else{ ?>
  20.     <div class="load-more" lastID="0">
  21.         No More Post To Load...
  22.     </div>
  23. <?php }
  24.     }
  25. }
  26. ?>
Database Configuration - dbConfig.php is a configuration of file, in this file we will create a database connection and select the data in MySQL database.
  1. <?php
  2. $dbhost = 'localhost';
  3. $dbusername = 'root';
  4. $dbpassword = '';
  5. $dbname = 'loaddata';
  6. $db = new mysqli($dbhost, $dbusername, $dbpassword, $dbname);
  7. if ($db->connect_error) {
  8.     die("Error! Unable To Connect The Database: " . $db->connect_error);
  9. }
  10. ?>
PHP and HTML Form
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.         <title>Automatic Page Scroll Load Data</title>
  5.         <link type="text/css" rel="stylesheet" href="assets/css/style.css">
  6.         <link type="text/css" rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
  7.         <script src="assets/js/jquery.min.js"></script>
  8. <script type="text/javascript">
  9. $(document).ready(function(){
  10.         $(window).scroll(function(){
  11.                 var lastID = $('.load-more').attr('lastID');
  12.                 if ($(window).scrollTop() == $(document).height() - $(window).height() && lastID != 0){
  13.                         $.ajax({
  14.                                 type:'POST',
  15.                                 url:'data.php',
  16.                                 data:'id='+lastID,
  17.                                 beforeSend:function(html){
  18.                                         $('.load-more').show();
  19.                                 },
  20.                                 success:function(html){
  21.                                         $('.load-more').remove();
  22.                                         $('#postList').append(html);
  23.                                 }
  24.                         });
  25.                 }
  26.         });
  27. });
  28. </script>
  29. </head>
  30. <body>
  31. <?php
  32. include('dbConfig.php');
  33. ?>
  34. <div class="container">
  35. <div class="row">
  36.         <div class="col-lg-12">
  37.         <h1 align="center">Automatic Page Scroll Load Data</h1>
  38.                 <div class="post-list" id="postList">
  39.                 <?php
  40.                 $query = $db->query("SELECT * FROM data ORDER BY id DESC LIMIT 7");
  41.                 if($query->num_rows > 0){
  42.                         while($row = $query->fetch_assoc()){
  43.                                 $postID = $row["id"];
  44.                 ?>
  45.                         <div class="list-item" href="javascript:void(0);"><h2><?php echo $row['title']; ?></h2></div>
  46.                 <?php } ?>
  47.                         <div class="load-more" lastID="<?php echo $postID; ?>" style="display: none;">
  48.                                 <img src="assets/loading.gif"/>
  49.                         </div>
  50.                 <?php } ?>
  51.                 </div>
  52.         </div>
  53. </div>
  54. </div>
  55. </body>
  56. </html>
result Hope that you learn in this project and enjoy coding. Don't forget to LIKE & SHARE this website.

Add new comment