Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to implement load more dynamic results functionality using jQuery, Ajax Php and Database?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 7.12k
    Comment on it

    To display or load more results, whether it is commenting on a blog website, like or images on social media or products on eCommerce website the best UX pattern for this is either pagination, a Load more button or infinite scrolling. In this tutorial, we will see an "easy trick to implement load more dynamic results functionality using jQuery, Ajax Php, and Database".

     

    Basically, following tutorial require mainly 3 files to process and display more dynamic results. ( let's assume blog posts as results in our example).

     

    Each time user clicks on 'load more' button, two blog post will display one by one till all the blogs from the table are being fetched.

     

    One file is to hold database connection, Another one containing HTML/PHP/jquery code will request an ajax call on button click and another PHP file will process ajax request and send back the blog details to HTML file.

     

    /* Create Database connection to include in desired files as db_config.php file */

    1. <?php
    2. $db_host = 'localhost';
    3. $user = 'db_user_name';
    4. $pass = 'db_password';
    5. $db_name = 'db_name';
    6.  
    7. $con = mysqli_connect($db_host, $user, $pass, $db_name);
    8. ?>

     

    /* Create main index file to show blog posts with load more functionality.Initially there will be 2 blog post. */

    1. <html lang='en-US' xmlns="http://www.w3.org/1999/xhtml">
    2.     <head>
    3.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    4.         <title>Load more data using jQuery, Ajax and PHP </title>
    5.         <style type="text/css">
    6.             
    7.             .blog_item {
    8.                 padding: 1px 12px;
    9.                 background-color:#F1F1F1;
    10.                 margin: 5px 15px 2px;
    11.             }
    12.             
    13.             .blog_item a {
    14.                 text-decoration: none;
    15.                 padding-bottom: 2px;
    16.                 color: #0074a2;
    17.             }
    18.  
    19.             .show_more_container {
    20.                 margin: 15px 25px;
    21.             }
    22.  
    23.             .show_more {
    24.                 cursor: pointer;
    25.                 display: block;
    26.                 padding: 10px 0;
    27.                 text-align: center;
    28.                 font-weight:bold;
    29.             }
    30.         </style>
    31.         <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    32.         <script type="text/javascript">
    33.             $(document).ready(function(){
    34.                 $(document).on('click','.show_more',function(){
    35.                     var ID = $(this).attr('id');
    36.                     $('.show_more').hide();
    37.                     $.ajax({
    38.                         type:'POST',
    39.                         url:'ajax_more_posts.php',
    40.                         data:'pkBlogId='+ID,
    41.                         success:function(html){
    42.                             $('#show_more_container'+ID).remove();
    43.                             $('.blog_list').append(html);
    44.                         }
    45.                     });
    46.                 });
    47.             });
    48.         </script>
    49.     </head>
    50.  
    51.     <body>
    52.  
    53.         <?php
    54.         //include database config
    55.         include('db_config.php');
    56.         ?>
    57.  
    58.         <h1 style="text-align: center;">Load more blog items</h1>
    59.         <div class="blog_list">
    60.             <?php
    61.             //get rows query
    62.             $query = mysqli_query($con, "SELECT * FROM blogs ORDER BY pkBlogId DESC LIMIT 2");
    63.  
    64.             $numRows = mysqli_num_rows($query);
    65.             
    66.             if($numRows > 0){
    67.                 while($row = mysqli_fetch_assoc($query)){
    68.          $blog_id =     $row['pkBlogId']; ?>
    69.                       <div class="blog_item"><a href="javascript:void(0);"><h2><?php echo $row['blogTitle']; ?></h2></a></div>
    70.              <?php } ?>
    71.              <div class="show_more_container" id="show_more_container<?php echo $blog_id; ?>">
    72.              <span id="<?php echo $blog_id; ?>" class="show_more" title="Load more posts">Show more</span>
    73.              </div>
    74.          <?php } ?>
    75.         </div>
    76.     
    77.     </body>
    78. </html>

     

    /*   The following content will be used in ajax_more_posts.php file to request */

    1. <?php
    2. if(isset($_POST["pkBlogId"]) && !empty($_POST["pkBlogId"])){
    3.  
    4.     //include database config
    5.     include('db_config.php');
    6.  
    7.     
    8.     $queryAll = mysqli_query($con,"SELECT COUNT(*) as num_rows FROM blogs WHERE pkBlogId < ".$_POST['pkBlogId']." ORDER BY pkBlogId DESC");
    9.     $row = mysqli_fetch_assoc($queryAll);
    10.  
    11.     //count all rows except already displayed
    12.     $allRows = $row['num_rows'];
    13.  
    14.     $showLimit = 2;
    15.  
    16.     //get rows query
    17.     $query = mysqli_query($con, "SELECT * FROM blogs WHERE pkBlogId < ".$_POST['pkBlogId']." ORDER BY pkBlogId DESC LIMIT ".$showLimit);
    18.  
    19.     $numRows = mysqli_num_rows($query);
    20.  
    21.     if($numRows > 0){
    22.      while($row = mysqli_fetch_assoc($query)){
    23.      $blog_id = $row["pkBlogId"]; ?>
    24.      <div class="blog_item"><a href="javascript:void(0);"><h2><?php echo $row["blogTitle"]; ?></h2></a></div>
    25.         <?php } ?>
    26.         <?php if($allRows > $showLimit){ ?>
    27.          <div class="show_more_container" id="show_more_container<?php echo $blog_id; ?>">
    28.          <a id="<?php echo $blog_id; ?>" class="show_more" title="Load more posts">Show more</a>
    29.          <span class="loding" style="display: none;"><span class="loding_txt">Loading</span></span>
    30.          </div>
    31.         <?php } ?>
    32.     <?php
    33. }
    34. }
    35. ?>

     

    Well, isn’t it easy? Please feel free to share your thoughts and questions in the comment section below.

    Happy Coding !!!

    How to implement load more dynamic results functionality using jQuery, Ajax Php and Database?

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: