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
|
<?php
session_start();
require_once "dbCredentials.php";
require_once "consts.php";
error_reporting(E_ALL);
ini_set('display_errors', 1);
checkLoggedIn();
if ($_SESSION["user_type"] !== 1) {
header("Location: main.php");
}
$pdo = new PDO("mysql:dbname=$dbName;host=$dbHost;charset=utf8mb4", $dbUser, $dbPassword);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$stmt = $pdo->prepare("SELECT * FROM `cms_card`");
$stmt->execute();
$cards = $stmt->fetchAll(PDO::FETCH_ASSOC);
function strStartsWith(string $needle, string $haystack)
{
return substr($haystack, 0, strlen($needle)) === $needle;
}
if (isset($_GET["editType"])) {
$type = explode('!', $_GET["editType"]);
if ($type[0] === "Add" || $type[0] === "Edit") {
if ($type[0] === "Edit") {
$stmt = $pdo->prepare("SELECT * FROM `cms_card` WHERE `id` = :id");
$stmt->execute(["id" => $type[1]]);
$card = $stmt->fetchAll(PDO::FETCH_ASSOC)[0];
} else {
$card = [
"title" => "",
"body" => "",
];
}
$_SESSION["edited_card"] = $card;
header("Location: cardEdit.php");
} elseif ($type[0] === "Remove") {
$stmt = $pdo->prepare("DELETE FROM `cms_card` WHERE `id` = :id");
$stmt->execute(["id" => $type[1]]);
$card = $stmt->fetchAll(PDO::FETCH_ASSOC);
header("Location: cardsList.php");
}
exit();
}
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet">
<title>Card List</title>
</head>
<body class="bg-dark"><br>
<div class="container">
<table class="table table-dark table-bordered text-center" style="border: 1px solid white;">
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Body</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php
foreach ($cards as $card) {
if (strlen($card["body"]) > 25)
$title = substr($card["title"], 0, 25) . "...";
else
$title = $card["title"];
if (strlen($card["body"]) > 150)
$body = substr($card["body"], 0, 150) . "...";
else
$body = $card["body"];
echo "<tr>
<td>{$card["id"]}</td>
<td>$title</td>
<td style='width: 700px; border: 0; display: inline-block; overflow-wrap: break-word;'>$body</td>
<td style='width: 280px;'>
<a class='btn btn-primary' href='cardsList.php?editType=Edit!{$card["id"]}'>Edit <i class='fas fa-edit'></i></a>
<a class='btn btn-danger' href='cardsList.php?editType=Remove!{$card["id"]}'>Remove <i class='fas fa-trash'></i></a>
</td>
</tr>";
}
?>
</tbody>
</table>
<a class="btn btn-success" style="float: right;" href="cardsList.php?editType=Add">Add</a>
<a class="btn btn-primary" style="float: left;" href="main.php">Go to main</a>
</div>
<!-- Optional JavaScript; choose one of the two! -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
</body>
</html>
|