-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathblog_scrolling.php
85 lines (62 loc) · 1.96 KB
/
blog_scrolling.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
//Infinite scrolling for blog posts
function is_ajax_request() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
}
// Query DB for posts
if(!defined('DB_USER')){
require "config.php";
try {
$conn = new PDO("mysql:host=". DB_HOST. ";dbname=". DB_DATABASE , DB_USER, DB_PASSWORD);
} catch (PDOException $pe) {
die("Could not connect to the database " . DB_DATABASE . ": " . $pe->getMessage());
}
}
try {
$query = "SELECT * FROM posts WHERE name LIKE :search";
$q = $conn->prepare($query);
$q->execute(array(':search' => $search));
$q->setFetchMode(PDO::FETCH_ASSOC);
$data = $q->fetchAll();
} catch (PDOException $e) {
throw $e;
}
if(!is_ajax_request()) {
exit;
}
?>
<script>
let container = document.querySelector('.blog-posts');
let load_more = document.querySelector('#load-more');
function showLoading() {
var loader = document.querySelector('.loading')
loader.style.display = 'block';
}
function hideLoading(){
var loader = document.querySelector('.loading')
loader.style.display = 'none';
}
function showLoadMore() {
load_more.style.display = 'inline';
}
function hideLoadMore(){
load_more.style.display = 'none';
}
function loadMore() {
showLoading();
hideLoadMore();
let xhr = new XMLHttpRequest();
xhr.open('GET', 'blog.php?page=1', true);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
let result = xhr.responseText;
hideLoading();
showLoadMore();
}
};
xhr.send();
}
load_more.addEventListener("click", loadMore);
loadMore();
</script>