diff options
Diffstat (limited to 'cardsList.php')
| -rw-r--r-- | cardsList.php | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/cardsList.php b/cardsList.php new file mode 100644 index 0000000..7e2ffc5 --- /dev/null +++ b/cardsList.php @@ -0,0 +1,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>
\ No newline at end of file |
