PHP-databaskonstruktion

Log | Files | Refs

commit fd03122ec1b5c3ac7523814fc744d6f7c21f8f9e
parent 11f6e4e0f98f62b9b8e255ade2ccb28b9f4a545a
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Mon, 25 Sep 2023 07:42:15 +0000

Implemented modalBuilder.php.

Diffstat:
MComponents.php | 66++++++++++++++++++++++--------------------------------------------
Mincidents.php | 2+-
Mindex.php | 1-
AmodalBuilder.php | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atest.php | 29+++++++++++++++++++++++++++++
5 files changed, 132 insertions(+), 46 deletions(-)

diff --git a/Components.php b/Components.php @@ -1,4 +1,6 @@ <?php + include 'modalBuilder.php'; + function displayTable($tableName) { $db = Database::getInstance(); $pdo = $db->getPdo(); @@ -13,7 +15,7 @@ $output .= "<thead class='thead-dark'><tr>"; foreach($row as $key => $value) { if(!is_numeric($key)) { // Avoid displaying numeric indices - $output .= "<th>" . htmlspecialchars($key) . "</th>"; + $output .= "<th>" . safeHmlspecialchars($key) . "</th>"; } } $output .= "</tr></thead><tbody>"; @@ -22,7 +24,7 @@ $output .= "<tr>"; foreach($row as $key => $value) { if(!is_numeric($key)) { // Avoid displaying numeric indices - $output .= "<td>" . htmlspecialchars($value) . "</td>"; + $output .= "<td>" . safeHmlspecialchars($value) . "</td>"; } } $output .= "</tr>"; @@ -33,6 +35,10 @@ echo $output; } + function safeHmlspecialchars($value) { + return empty($value) ? "" : htmlspecialchars($value); + } + function generateNavbar() { $currentPage = basename($_SERVER['SCRIPT_NAME']); $pages = [ @@ -83,6 +89,7 @@ function generateFooter() { $year = date('Y'); echo " + <br><br><br><br> <footer class='footer py-3 bg-dark text-white text-center'> <div class='container'> <p>&copy;{$year} PUCKO-PORTAL. All rights reserved.</p> @@ -136,11 +143,21 @@ insertDataIntoTable($pdo, $tableName); redirectToCurrentPage(); } - + $columns = fetchTableColumns($pdo, $tableName); - echo generateAddDataButton() . generateStylizedModal($tableName, $columns); + + $modalBuilder = (new ModalBuilder()) + ->setModalId('insertModal') + ->setTableName($tableName); + + foreach ($columns as $column) { + $modalBuilder->addColumn($column); + } + + echo $modalBuilder->generateOpenButton("Add Data"); + echo $modalBuilder->build(); } - + function isFormSubmitted() { return $_SERVER['REQUEST_METHOD'] === 'POST'; } @@ -159,43 +176,4 @@ $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_COLUMN); } - - function generateAddDataButton() { - return "<button type='button' class='btn btn-primary' data-toggle='modal' data-target='#insertModal'> - Add Data - </button>"; - } - - function generateStylizedModal($tableName, $columns) { - $modalStart = "<form action='' method='POST'> - <div class='modal fade' id='insertModal' tabindex='-1' role='dialog' aria-labelledby='insertModalLabel' aria-hidden='true'> - <div class='modal-dialog' role='document'> - <div class='modal-content rounded'> - <div class='modal-header'> - <button type='button' class='close ml-0' data-dismiss='modal' aria-label='Close'> - <span aria-hidden='true'>&times;</span> - </button> - </div> - <div class='modal-body'>"; - - $modalBody = ''; - foreach ($columns as $column) { - $modalBody .= "<div class='form-group'> - <label for='$column'>$column</label> - <input type='text' class='form-control' id='$column' name='$column' placeholder='$column' required> - </div>"; - } - - $modalEnd = "</div> - <div class='modal-footer'> - <input type='hidden' name='tableName' value='$tableName'> - <button type='submit' class='btn btn-primary'>Save</button> - </div> - </div> - </div> - </form>"; - - return $modalStart . $modalBody . $modalEnd; - } - ?> \ No newline at end of file diff --git a/incidents.php b/incidents.php @@ -1,6 +1,6 @@ <?php require 'Database.php'; - include 'Components.php'; + include 'Components.php'; Database::getInstance('mysql', 'a22willi', 'root', 'Safiren1'); generateHead(); diff --git a/index.php b/index.php @@ -25,7 +25,6 @@ echo "</div>"; - # --- FOOTER --- # generateFooter(); ?> diff --git a/modalBuilder.php b/modalBuilder.php @@ -0,0 +1,79 @@ +<?php + class ModalBuilder { + private $tableName; + private $columns = []; + private $dropdownColumns = []; + private $modalId; + + public function setTableName($tableName) { + $this->tableName = $tableName; + return $this; + } + + public function setModalId($modalId = "modal") { + $this->modalId = $modalId; + return $this; + } + + public function addColumn($column) { + $this->columns[] = $column; + return $this; + } + + public function addDropdownColumn($column, $values) { + $this->dropdownColumns[$column] = $values; + return $this; + } + + public function generateOpenButton($label = "Open Modal") { + echo "<button type='button' class='btn btn-primary' data-toggle='modal' data-target='#{$this->modalId}'>{$label}</button>"; + } + + public function build() { + $modalStart = "<form action='' method='POST'> + <div class='modal fade' id='{$this->modalId}' tabindex='-1' role='dialog' aria-labelledby='insertModalLabel' aria-hidden='true'> + <div class='modal-dialog' role='document'> + <div class='modal-content rounded'> + <div class='modal-header'> + <button type='button' class='close ml-0' data-dismiss='modal' aria-label='Close'> + <span aria-hidden='true'>&times;</span> + </button> + <h5 class='modal-title'>Insert into: {$this->tableName}</h5> + </div> + <div class='modal-body'>"; + + $modalBody = ''; + foreach ($this->columns as $column) { + $modalBody .= "<div class='form-group'> + <label for='$column'>$column</label> + <input type='text' class='form-control' id='$column' name='$column' placeholder='$column' required> + </div>"; + } + + foreach ($this->dropdownColumns as $column => $values) { + $options = ''; + foreach ($values as $value) { + $options .= "<option value='{$value}'>{$value}</option>"; + } + $modalBody .= "<div class='form-group'> + <label for='$column'>$column</label> + <select class='form-control' id='$column' name='$column' required> + $options + </select> + </div>"; + } + + $modalEnd = "</div> + <div class='modal-footer'> + <input type='hidden' name='tableName' value='{$this->tableName}'> + <button type='submit' class='btn btn-primary'>Save</button> + </div> + </div> + </div> + </div> + </form>"; + + echo $modalStart . $modalBody . $modalEnd; + } + } +?> +\ No newline at end of file diff --git a/test.php b/test.php @@ -0,0 +1,28 @@ +<?php + + + include 'Components.php'; + include 'modalBuilder.php'; + + generateHead(); + echo '<body>'; + $modalBuilder1 = (new ModalBuilder()) + ->setModalId('modal1') + ->setTableName('SampleTable1') + ->addColumn('FirstName') + ->addColumn('LastName') + ->addDropdownColumn('Foreign', ['hej', 'test', 'hello', 'asddfsdf']); + + $modalBuilder2 = (new ModalBuilder()) + ->setModalId('modal2') + ->setTableName('SampleTable2') + ->addColumn('Age'); + + $modalBuilder1->generateOpenButton("Open Modal 1"); + $modalBuilder1->build(); + + $modalBuilder2->generateOpenButton("Open Modal 2"); + $modalBuilder2->build(); + + echo '</body>'; +?> +\ No newline at end of file