-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
199 lines (173 loc) · 7.65 KB
/
index.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/*******w********
Name: John Kelvin A. Valerio
Date: 03/13/23
Description: Project
****************/
require('connect.php');
session_start();
// session_unset();
// session_destroy();
// Get the current page number, or default to 1 if not set
$page = isset($_GET['page']) ? $_GET['page'] : 1;
// Define how many results to show per page
$resultsPerPage = 5;
// Calculate the offset based on the current page and number of results per page
$offset = ($page - 1) * $resultsPerPage;
// SQL query to retrieve the desired range of rows
$query = "SELECT * FROM content_post ORDER BY post_id DESC LIMIT $offset, $resultsPerPage";
// Prepare and execute the query
$statement = $db->prepare($query);
$statement->execute();
// Count the total number of rows in the table
$totalResults = $db->query("SELECT COUNT(*) FROM content_post")->fetchColumn();
// Calculate the total number of pages
$totalPages = ceil($totalResults / $resultsPerPage);
// Build the pagination links
$pagination = "";
if ($totalPages > 1) {
for ($i = 1; $i <= $totalPages; $i++) {
$pagination .= "<a href=\"index.php?page=$i\">$i</a> ";
}
}
// SQL is written as a String.
$query2 = "SELECT * FROM categories ORDER BY categorie_id DESC";
// A PDO::Statement is prepared from the query.
$statement2 = $db->prepare($query2);
// Execution on the DB server is delayed until we execute().
$statement2->execute();
$tagList = array();
if ($statement2->rowCount() > 0) {
while ($row = $statement2->fetch()) {
$tagList[$row['categorie_id']] = $row['categorie_name'];
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="main.css">
<title>Drip Book</title>
</head>
<body>
<!-- Remember that alternative syntax is good and html inside php is bad -->
<div id="wrapper">
<div id="header">
<div id="user_header">
<h1> Kelvin's Blog - Index </h1>
<?php if (isset($_SESSION['user_id'])): ?>
<form action="post.php" method="post">
<p>
<input type="submit" name="logout" value="logout"
onclick="return confirm('Are you sure you want to logout?')">
</p>
</form>
<?php else: ?>
<form action="post.php" method="post">
<p>
<input type="submit" name="login" value="login" />
</p>
</form>
<?php endif ?>
</div>
<ul class="menu">
<li>
<a href="index.php" class="active">Home</a>
</li>
<li>
<a href="authenticate.php">New Post</a>
</li>
<li>
<a href="registration.php">Register User</a>
</li>
<? echo ("THIS IS SESSION LVL" + $_SESSION['user_lvl']) ?>
<?php if (isset($_SESSION['user_lvl'])): ?>
<li>
<a href="createCategories.php">Categories</a>
</li>
<?php endif ?>
<?php if (isset($_SESSION['user_lvl']) && $_SESSION['user_lvl'] === 1): ?>
<li>
<a href="userList.php">User list</a>
</li>
<?php endif ?>
</ul>
</div>
<!-- Add a form element to accept user's search query -->
<form method="GET" action="index.php">
<input type="text" name="search" placeholder="Search...">
<label for="tag">Select a tag:</label>
<select name="tag" id="tag">
<option value="">--Please chose a tag--</option>
<?php foreach ($tagList as $tag => $value): ?>
<option value="<?php echo $tag ?>"><?php echo $value ?></option>
<?php endforeach ?>
</select>
<button type="submit">Search</button>
</form>
<!-- Display the search results -->
<div id="all_blogs">
<?php if ($statement->rowCount() > 0): ?>
<?php while ($row = $statement->fetch()): ?>
<!-- Filter the results by the post's title -->
<?php if (empty($_GET['tag']) || $_GET['tag'] == $row['categorie_id'] || $_GET['tag'] == ""): ?>
<?php if (empty($_GET['search']) || strpos($row['title'], $_GET['search']) !== false): ?>
<ul class="menu">
<li>
<div class="blog_post">
<h2>
<a href="display.php?id=<?= $row['post_id'] ?>&slug=<?=$row['slug']?>" class="title-link"><?= $row['title'] ?></a>
</h2>
<p>
<small>
<?= date("F d, Y, h:ia", strtotime($row['created_at_date'])) ?>
<a href="edit.php?id=<?= $row['post_id'] ?>">edit</a>
</small>
</p>
</div>
<?php if (strlen($row['content']) >= 200): {
$content = mb_substr($row['content'], 0, 200) . "<a href='display.php?id=" . $row['post_id'] . "'>Read Full Post</a>";
}
?>
<?php else: ?>
<?php $content = $row['content'] ?>
<?php endif ?>
<?php if(!empty($row['image_path'])): ?>
<div>
<img src="<?php echo $row['image_path']; ?>" alt="No preview">
</div>
<?php endif?>
<div class='blog_content'>
<?= $content ?>
</div>
<div>
<?php foreach ($tagList as $tag => $value): ?>
<?php if ($row['categorie_id'] === $tag): ?>
<input class="tagButton" type="submit" name="tag" value=<?php echo $value ?>>
<?php endif ?>
<?php endforeach ?>
</div>
</li>
</ul>
<?php endif ?>
<?php endif ?>
<?php endwhile ?>
<!-- Display the pagination links -->
<?php if ($totalPages > 1): ?>
<div class="pagination">
<?= $pagination ?>
</div>
<?php endif ?>
<?php else: ?>
<h2>No blogs found.</h2>
<?php endif ?>
</div>
<div id="footer">
Copywrong 2023 - No Rights Reserved
</div>
</div>
</body>
</html>