Database Configuration
Database Configuration
php)
The dbConfig.php file is used to connect and select the MySQL database. Specify the
database hostname ($dbHost), username ($dbUsername), password ($dbPassword), and
name ($dbName) as per your MySQL credentials.
<?php
//DB details
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = 'root';
$dbName = 'codexworld';
if ($db->connect_error) {
die("Unable to connect database: " . $db->connect_error);
}
?>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"
></script>
The jQuery scroll() method will be used to detect the page scroll and Ajax request will
be initiated when user scrolling down to bottom of the page. On Ajax request, the last
displayed post ID (lastID) will be sent to the getData.php file. Once Ajax success
method returns the posts data, the content HTML will append to posts list.
<script type="text/javascript">
$(document).ready(function(){
$(window).scroll(function(){
var lastID = $('.load-more').attr('lastID');
if(($(window).scrollTop() == $(document).height() -
$(window).height()) && (lastID != 0)){
$.ajax({
type:'POST',
url:'getData.php',
data:'id='+lastID,
beforeSend:function(){
$('.load-more').show();
},
success:function(html){
$('.load-more').remove();
$('#postList').append(html);
}
});
}
});
});
</script>
<div id="postList">
<?php
// Include the database configuration file
require 'dbConfig.php';
<?php
if(!empty($_POST["id"])){
//Get last ID
$lastID = $_POST['id'];
CSS Code
The following CSS code is used to specify the style of the posts list.
#postList{
margin-bottom:20px;
}
.list-item {
background-color: #F1F1F1;
margin: 5px 15px 2px;
padding: 2px;
font-size: 14px;
line-height: 1.5;
height: 120px;
}
.list-item h4 {
color: #0074a2;
margin-left: 10px;
}
.load-more {
margin: 15px 25px;
cursor: pointer;
padding: 10px 0;
text-align: center;
font-weight:bold;
}