commit 1c9b08f6835ce28d51b9e7775eb485c78d6a30cd
parent a7b202e9f121949e81b06e1b1406c7ee632a48d0
Author: William Lindholm <william_lindholm@outlook.com>
Date: Tue, 26 Sep 2023 21:57:54 +0000
Code cleanup and refactoring.
Diffstat:
14 files changed, 247 insertions(+), 454 deletions(-)
diff --git a/Components.php b/Components.php
@@ -1,273 +0,0 @@
-<?php
- include 'modalBuilder.php';
-
- function displayTable($tableName) {
- $db = Database::getInstance();
- $pdo = $db->getPdo();
-
- // Start the table
- $output = "<table class='table table-striped table-bordered'>";
-
- // Display table headers
- $firstRow = true;
- foreach($pdo->query('SELECT * FROM ' . $tableName . ';') AS $row) {
- if($firstRow) {
- $output .= "<thead class='thead-dark'><tr>";
- foreach($row as $key => $value) {
- if(!is_numeric($key)) { // Avoid displaying numeric indices
- $output .= "<th>" . safeHmlspecialchars($key) . "</th>";
- }
- }
- $output .= "</tr></thead><tbody>";
- $firstRow = false;
- }
- $output .= "<tr>";
- foreach($row as $key => $value) {
- if(!is_numeric($key)) { // Avoid displaying numeric indices
- $output .= "<td>" . safeHmlspecialchars($value) . "</td>";
- }
- }
- $output .= "</tr>";
- }
- $output .= "</tbody></table>";
-
- // Return the generated table
- echo $output;
- }
-
- function safeHmlspecialchars($value) {
- return empty($value) ? "" : htmlspecialchars($value);
- }
-
- function generateNavbar() {
- $currentPage = basename($_SERVER['SCRIPT_NAME']);
- $pages = [
- 'index.php' => 'Agents',
- 'incidents.php' => 'Incidents',
- 'operations.php' => 'Operations',
- 'terrain.php' => 'Terrain'
- ];
-
- echo "<nav class='navbar navbar-expand-lg navbar-dark bg-dark'>
- <a class='navbar-brand' href='#'>PUCKO-PORTAL</a>
- <button class='navbar-toggler' type='button' data-toggle='collapse' data-target='#navbarNav' aria-controls='navbarNav' aria-expanded='false' aria-label='Toggle navigation'>
- <span class='navbar-toggler-icon'></span>
- </button>
- <div class='collapse navbar-collapse' id='navbarNav'>
- <ul class='navbar-nav'>";
-
- foreach ($pages as $file => $name) {
- $active = ($currentPage == $file) ? 'active' : '';
- echo "<li class='nav-item $active'>
- <a class='nav-link' href='./$file'>$name</a>
- </li>";
- }
-
- echo "</ul>
- </div>
- </nav>";
- }
-
- function generateHead() {
- echo "
- <!DOCTYPE html>
- <html lang='en'>
- <head>
- <meta charset='UTF-8'>
- <meta name='viewport' content='width=device-width, initial-scale=1.0'>
- <title>PUCKO-PORTAL</title>
- <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css'>
- <link rel='stylesheet' type='text/css' href='stylesheet.css'>
-
- <script defer src='https://code.jquery.com/jquery-3.5.1.slim.min.js'></script>
- <script defer src='https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js'></script>
- <script defer src='https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js'></script>
- </head>
- ";
- }
-
- 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>©{$year} PUCKO-PORTAL. All rights reserved.</p>
- </div>
- </footer>
- ";
- }
-
- function hasInsertPrivilege($tableName) {
- // Get the PDO instance from the Database singleton
- $db = Database::getInstance();
- $pdo = $db->getPdo();
-
- // Query the database to get the grants for the current user
- $sqlCheckPrivileges = "SHOW GRANTS FOR CURRENT_USER()";
- $stmtCheck = $pdo->prepare($sqlCheckPrivileges);
- $stmtCheck->execute();
- $grants = $stmtCheck->fetchAll(PDO::FETCH_COLUMN);
-
- // Check if any of the grants include the INSERT privilege for the specified table or for all tables
- foreach ($grants as $grant) {
- if (strpos($grant, "GRANT INSERT ON *.*") !== false) {
- return true; // Global insert privilege
- }
- if (strpos($grant, "GRANT INSERT ON `{$tableName}`") !== false || strpos($grant, "GRANT INSERT ON `$tableName`.*") !== false) {
- return true; // Specific table or database-wide insert privilege
- }
- }
-
- return false;
- }
-
- function insertDataIntoTable($pdo, $tableName) {
- $columns = array_keys($_POST);
- unset($columns[array_search('tableName', $columns)]); // Exclude tableName from columns
-
- $placeholders = rtrim(str_repeat('?,', count($columns)), ',');
- $sql = "INSERT INTO $tableName (" . implode(',', $columns) . ") VALUES ($placeholders)";
-
- $stmt = $pdo->prepare($sql);
- $values = array_values($_POST);
- array_pop($values); // Exclude tableName from values
- $stmt->execute($values);
- }
-
- function getPrimaryKeysOfReferencedTable($pdo, $referencedTable, $referencedColumn) {
- $stmt = $pdo->prepare("SELECT $referencedColumn FROM $referencedTable");
- $stmt->execute();
- return $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
- }
-
- function generateInsertFunction($tableName) {
- $db = Database::getInstance();
- $pdo = $db->getPdo();
-
- if (isFormSubmitted() && isTableNameSet()) {
- insertDataIntoTable($pdo, $tableName);
- redirectToCurrentPage();
- }
-
- $columns = fetchTableColumns($pdo, $tableName);
- $foreignKeys = getForeignKeys($tableName);
-
- $modalBuilder = (new ModalBuilder())
- ->setModalId('insertModal')
- ->setTableName($tableName);
-
- $handledForeignKeys = []; // To keep track of which foreign keys have already been handled
-
- foreach ($columns as $column) {
- $isForeignKey = false;
-
- foreach ($foreignKeys as $foreignKey) {
- if ($foreignKey['COLUMN_NAME'] == $column) {
- if (in_array($column, $handledForeignKeys)) {
- continue; // Skip if this foreign key column is already handled
- }
- $referencedTable = $foreignKey['REFERENCED_TABLE_NAME'];
- $primaryKeyValues = fetchPrimaryKeyValues($pdo, $referencedTable);
- $modalBuilder->addDropdownColumn($column, $primaryKeyValues);
- $isForeignKey = true;
-
- // Mark the current foreign key column as handled
- $handledForeignKeys[] = $column;
-
- // Check if any other columns reference the same table (i.e., composite key)
- foreach ($foreignKeys as $fk) {
- if ($fk['REFERENCED_TABLE_NAME'] == $referencedTable) {
- $handledForeignKeys[] = $fk['COLUMN_NAME'];
- }
- }
-
- break;
- }
- }
-
- if (!$isForeignKey) {
- $modalBuilder->addColumn($column);
- }
- }
-
- echo $modalBuilder->generateOpenButton("Add Data");
- echo $modalBuilder->build();
- }
-
- function fetchPrimaryKeyValues($pdo, $table) {
- // Get primary key columns of the table
- $stmt = $pdo->prepare("SHOW KEYS FROM $table WHERE Key_name = 'PRIMARY'");
- $stmt->execute();
- $primaryKeys = $stmt->fetchAll(PDO::FETCH_ASSOC);
-
- // If there's only one primary key, return its values
- if (count($primaryKeys) === 1) {
- $column = $primaryKeys[0]['Column_name'];
- $stmt = $pdo->prepare("SELECT $column FROM $table");
- $stmt->execute();
- return $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
- }
-
- // For composite primary keys
- $columns = array_map(function ($item) {
- return $item['Column_name'];
- }, $primaryKeys);
-
- $selectColumns = implode(", ", $columns);
- $stmt = $pdo->prepare("SELECT $selectColumns FROM $table");
- $stmt->execute();
-
- $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
- $compositeKeyValues = [];
-
- foreach ($results as $row) {
- $compositeKeyValues[] = implode("-", $row); // Combining multiple column values
- }
-
- return $compositeKeyValues;
- }
-
- function getForeignKeys($table) {
- // Get the database instance and PDO object
- $db = Database::getInstance();
- $pdo = $db->getPdo();
-
- // Fetch the current database name
- $currentDatabase = $pdo->query('SELECT DATABASE()')->fetchColumn();
-
- // Prepare and execute the statement to fetch foreign keys
- $stmt = $pdo->prepare("
- SELECT COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
- FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
- WHERE TABLE_SCHEMA = :databaseName
- AND TABLE_NAME = :tableName
- AND REFERENCED_TABLE_NAME IS NOT NULL;
- ");
-
- $stmt->bindParam(':databaseName', $currentDatabase);
- $stmt->bindParam(':tableName', $table);
- $stmt->execute();
-
- return $stmt->fetchAll(PDO::FETCH_ASSOC);
- }
-
- function isFormSubmitted() {
- return $_SERVER['REQUEST_METHOD'] === 'POST';
- }
-
- function isTableNameSet() {
- return isset($_POST['tableName']);
- }
-
- function redirectToCurrentPage() {
- header('Location: ' . $_SERVER['PHP_SELF']);
- exit;
- }
-
- function fetchTableColumns($pdo, $tableName) {
- $stmt = $pdo->prepare('DESCRIBE ' . $tableName);
- $stmt->execute();
- return $stmt->fetchAll(PDO::FETCH_COLUMN);
- }
-?>
-\ No newline at end of file
diff --git a/Database.php b/Database.php
@@ -1,36 +0,0 @@
-<?php
-
-// Singleton class for PDO connection
-
-class Database {
- private static $instance = null;
- private $pdo;
-
- private function __construct($host, $dbname, $username, $password) {
- try {
- $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
- } catch (PDOException $e) {
- die('Connection failed: ' . $e->getMessage());
- }
- }
-
- public static function getInstance($host = null, $dbname = null, $username = null, $password = null) {
- if (self::$instance === null) {
- if ($host === null || $dbname === null || $username === null || $password === null) {
- throw new Exception("Database connection parameters are required for the first call to getInstance.");
- }
- self::$instance = new Database($host, $dbname, $username, $password);
- }
- return self::$instance;
- }
-
- public function getPdo() {
- return $this->pdo;
- }
-}
-
-// Usage example:
-// $db = Database::getInstance('localhost', 'mydb', 'root', 'password');
-// $pdo = $db->getPdo();
-
-?>
-\ No newline at end of file
diff --git a/components.php b/components.php
@@ -0,0 +1,32 @@
+<?php
+ function displayTable($tableName) {
+ $db = dbconnection::getInstance();
+ $pdo = $db->getPdo();
+
+ $output = "<div class='overflow-auto'><table class='table table-striped table-bordered'>";
+
+ $firstRow = true;
+ foreach($pdo->query('SELECT * FROM ' . $tableName . ';') AS $row) {
+ if($firstRow) {
+ $output .= "<thead class='thead-dark'><tr>";
+ foreach($row as $key => $value) {
+ if(!is_numeric($key)) {
+ $output .= "<th>" . $key . "</th>";
+ }
+ }
+ $output .= "</tr></thead><tbody>";
+ $firstRow = false;
+ }
+ $output .= "<tr>";
+ foreach($row as $key => $value) {
+ if(!is_numeric($key)) {
+ $output .= "<td>" . $value . "</td>";
+ }
+ }
+ $output .= "</tr>";
+ }
+ $output .= "</tbody></table></div>";
+
+ return $output;
+ }
+?>
+\ No newline at end of file
diff --git a/dbconnection.php b/dbconnection.php
@@ -0,0 +1,33 @@
+<?php
+ // Singleton class for PDO connection
+ class dbconnection {
+ private static $instance = null;
+ private $pdo;
+
+ private function __construct($host, $dbname, $username, $password) {
+ try {
+ $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
+ } catch (PDOException $e) {
+ die('Connection failed: ' . $e->getMessage());
+ }
+ }
+
+ public static function getInstance($host = null, $dbname = null, $username = null, $password = null) {
+ if (self::$instance === null) {
+ if ($host === null || $dbname === null || $username === null || $password === null) {
+ throw new Exception("Database connection parameters are required for the first call to getInstance.");
+ }
+ self::$instance = new dbconnection($host, $dbname, $username, $password);
+ }
+ return self::$instance;
+ }
+
+ public function getPdo() {
+ return $this->pdo;
+ }
+
+ public function getDbName() {
+ return $this->pdo->query('SELECT DATABASE()')->fetchColumn();
+ }
+ }
+?>
+\ No newline at end of file
diff --git a/dbhelper.php b/dbhelper.php
@@ -0,0 +1,21 @@
+<?php
+ function getColumnValues($table, $columnName) {
+ $db = dbconnection::getInstance();
+ $pdo = $db->getPdo();
+
+ try {
+ $query = "SELECT DISTINCT $columnName FROM $table";
+ $stmt = $pdo->prepare($query);
+ $stmt->execute();
+
+ $values = $stmt->fetchAll(PDO::FETCH_ASSOC);
+
+ $values = array_column($values, $columnName);
+
+ return $values;
+ } catch (PDOException $e) {
+ echo "Error: " . $e->getMessage();
+ return [];
+ }
+ }
+?>
+\ No newline at end of file
diff --git a/imports.php b/imports.php
@@ -0,0 +1,7 @@
+<?php
+ require 'dbconnection.php';
+ include 'dbhelper.php';
+ include 'logging.php';
+ include 'components.php';
+ include 'modalBuilder.php';
+?>
+\ No newline at end of file
diff --git a/incidents.php b/incidents.php
@@ -1,29 +1,21 @@
<?php
- require 'Database.php';
- include 'Components.php';
+ require 'imports.php';
+ dbconnection::getInstance('mysql', 'a22willi', 'root', 'Safiren1');
- Database::getInstance('mysql', 'a22willi', 'root', 'Safiren1');
- generateHead();
-?>
+ $pageContent = '';
-<body>
- <?php
- # --- HEAD --- #
- generateNavbar();
+ $pageContent .= "<h3>FieldAgents</h3>" . displayTable("Incident");
- # --- TABLES --- #
- echo "<div class=\"m-5\">";
+ $modalBuilder = (new ModalBuilder())
+ ->setModalId('insertModal')
+ ->setTableName("Incident")
+ ->addColumn("RegionName")
+ ->addColumn("Location")
+ ->addDropdownColumn("Incident", ['True', 'False']) #add GetCompositeKeyValues();
+ ->addDropdownColumn("Terrain", getColumnValues("Terrain", "TerrainCode")); #add GetColumnValues(table, column);
- echo "<h2>Incidents</h2>";
- displayTable("Incident");
+ $pageContent .= $modalBuilder->build();
+ $pageContent .= $modalBuilder->generateOpenButton("Create incident");
- generateInsertFunction("Incident");
-
- echo "</div>";
-
- # --- FOOTER --- #
- generateFooter();
- ?>
-</body>
-
-</html>
-\ No newline at end of file
+ include 'pageTemplate.php';
+?>
+\ No newline at end of file
diff --git a/index.php b/index.php
@@ -1,33 +1,25 @@
<?php
- require 'Database.php';
- include 'Components.php';
-
- Database::getInstance('mysql', 'a22willi', 'root', 'Safiren1');
- generateHead();
-?>
-
-<body>
- <?php
- # --- HEAD --- #
- generateNavbar();
-
- # --- TABLES --- #
- echo "<div class=\"m-5\">";
-
- echo "<h2>FieldAgents</h2>";
- displayTable("FieldAgents");
-
- echo "<h2>Managers</h2>";
- displayTable("Managers");
-
- echo "<h2>GroupLeaders</h2>";
- displayTable("GroupLeaders");
-
- echo "</div>";
-
- # --- FOOTER --- #
- generateFooter();
- ?>
-</body>
-
-</html>
-\ No newline at end of file
+ require 'imports.php';
+ dbconnection::getInstance('mysql', 'a22willi', 'root', 'Safiren1');
+
+ $pageContent = '';
+
+ $pageContent .= "<h3>FieldAgents</h3>" . displayTable("FieldAgents");
+ $pageContent .= "<h3>GroupLeaders</h3>" . displayTable("GroupLeaders");
+ $pageContent .= "<h3>Managers</h3>" . displayTable("Managers");
+
+ $modalBuilder = (new ModalBuilder())
+ ->setModalId('insertModal')
+ ->setTableName("Agent")
+ ->addColumn("FirstName")
+ ->addColumn("LastName")
+ ->addColumn("Salary", true)
+ ->addDropdownColumn("IsFieldAgent", ['True', 'False'])
+ ->addDropdownColumn("IsGroupLeader", ['False', 'True'])
+ ->addDropdownColumn("IsManager", ['False', 'True']);
+
+ $pageContent .= $modalBuilder->build();
+ $pageContent .= $modalBuilder->generateOpenButton("Hire agent");
+
+ include 'pageTemplate.php';
+?>
+\ No newline at end of file
diff --git a/logging.php b/logging.php
@@ -0,0 +1,15 @@
+<?php
+ function logg($message) {
+ global $console_logs;
+ $console_logs[] = $message;
+ }
+
+ function outputConsoleLogs() {
+ global $console_logs;
+ echo '<script>';
+ foreach ($console_logs as $msg) {
+ echo "console.log(" . json_encode($msg) . ");";
+ }
+ echo '</script>';
+ }
+?>
+\ No newline at end of file
diff --git a/modalBuilder.php b/modalBuilder.php
@@ -3,6 +3,7 @@
private $tableName;
private $columns = [];
private $dropdownColumns = [];
+ private $requiredColumns = [];
private $modalId;
public function setTableName($tableName) {
@@ -15,8 +16,13 @@
return $this;
}
- public function addColumn($column) {
+ public function addColumn($column, $optional = false) {
$this->columns[] = $column;
+
+ if (!$optional) {
+ $this->requiredColumns[] = $column;
+ }
+
return $this;
}
@@ -26,7 +32,7 @@
}
public function generateOpenButton($label = "Open Modal") {
- echo "<button type='button' class='btn btn-primary' data-toggle='modal' data-target='#{$this->modalId}'>{$label}</button>";
+ return "<button type='button' class='btn btn-primary' data-toggle='modal' data-target='#{$this->modalId}'>{$label}</button>";
}
public function build() {
@@ -44,9 +50,10 @@
$modalBody = '';
foreach ($this->columns as $column) {
+ $required = in_array($column, $this->requiredColumns) ? 'required' : '';
$modalBody .= "<div class='form-group'>
<label for='$column'>$column</label>
- <input type='text' class='form-control' id='$column' name='$column' placeholder='$column' required>
+ <input type='text' class='form-control' id='$column' name='$column' placeholder='$column' $required>
</div>";
}
@@ -73,7 +80,7 @@
</div>
</form>";
- echo $modalStart . $modalBody . $modalEnd;
+ return $modalStart . $modalBody . $modalEnd;
}
}
?>
\ No newline at end of file
diff --git a/operations.php b/operations.php
@@ -1,28 +1,23 @@
<?php
- require 'Database.php';
- include 'Components.php';
+ require 'imports.php';
+ dbconnection::getInstance('mysql', 'a22willi', 'root', 'Safiren1');
- Database::getInstance('mysql', 'a22willi', 'root', 'Safiren1');
- generateHead();
-?>
+ $pageContent = '';
-<body>
- <?php
- # --- HEAD --- #
- generateNavbar();
+ $pageContent .= "<h3>Operations</h3>" . displayTable("Operation");
- # --- TABLES --- #
- echo "<div class=\"m-5\">";
+ $modalBuilder = (new ModalBuilder())
+ ->setModalId('insertModal')
+ ->setTableName("Operation")
+ ->addColumn("OperationName")
+ ->addColumn("StartDate")
+ ->addColumn("EndDate", true)
+ ->addColumn("SuccessRate", true)
+ ->addDropdownColumn("GroupLeader", ['True', 'False']) #add GetColumnValues(table, column);
+ ->addDropdownColumn("Incident", ['True', 'False']); #add GetCompositeKeyValues();
- echo "<h2>Operations</h2>";
- displayTable("Operation");
- generateInsertFunction("Operation");
+ $pageContent .= $modalBuilder->build();
+ $pageContent .= $modalBuilder->generateOpenButton("Create operation");
- echo "</div>";
-
- # --- FOOTER --- #
- generateFooter();
- ?>
-</body>
-
-</html>
-\ No newline at end of file
+ include 'pageTemplate.php';
+?>
+\ No newline at end of file
diff --git a/pageTemplate.php b/pageTemplate.php
@@ -0,0 +1,56 @@
+<?php
+ $pages = [
+ 'index.php' => 'Agents',
+ 'incidents.php' => 'Incidents',
+ 'operations.php' => 'Operations',
+ 'terrain.php' => 'Terrain'
+ ];
+?>
+
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <title>PUCKO-PORTAL</title>
+ <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
+ <link rel="stylesheet" type="text/css" href="stylesheet.css">
+ <script defer src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
+ <script defer src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"></script>
+ <script defer src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
+</head>
+
+<body>
+ <nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
+ <a class="navbar-brand" href="#">PUCKO-PORTAL</a>
+ <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
+ <span class="navbar-toggler-icon"></span>
+ </button>
+ <div class="collapse navbar-collapse" id="navbarNav">
+ <ul class="navbar-nav"><li class="nav-item ">
+ <?php
+ $currentPage = basename($_SERVER['SCRIPT_NAME']);
+ foreach ($pages as $file => $name) {
+ $active = ($currentPage == $file) ? 'active' : '';
+ echo "<li class='nav-item $active'>
+ <a class='nav-link' href='./$file'>$name</a>
+ </li>";
+ }
+ ?>
+ </ul>
+ </div>
+ </nav>
+
+ <br><br><br>
+
+ <div class='m-md-5 m-sm-3 m-1'>
+ <?php echo $pageContent; ?>
+ </div>
+
+ <br><br><br><br><br><br>
+
+ <footer class="footer py-3 bg-dark text-white text-center">
+ <div class="container">
+ <p>©<?php date('Y');?> PUCKO-PORTAL. All rights reserved.</p>
+ </div>
+ </footer>
+</body>
+\ No newline at end of file
diff --git a/terrain.php b/terrain.php
@@ -1,27 +1,10 @@
<?php
- require 'Database.php';
- include 'Components.php';
+ require 'imports.php';
+ dbconnection::getInstance('mysql', 'a22willi', 'root', 'Safiren1');
- Database::getInstance('mysql', 'a22willi', 'root', 'Safiren1');
- generateHead();
-?>
+ $pageContent = '';
-<body>
- <?php
- # --- HEAD --- #
- generateNavbar();
+ $pageContent .= "<h3>Terrain</h3>" . displayTable("Terrain");
- # --- TABLES --- #
- echo "<div class=\"m-5\">";
-
- echo "<h2>Terrain</h2>";
- displayTable("Terrain");
-
- echo "</div>";
-
- # --- FOOTER --- #
- generateFooter();
- ?>
-</body>
-
-</html>
-\ No newline at end of file
+ include 'pageTemplate.php';
+?>
+\ No newline at end of file
diff --git a/test.php b/test.php
@@ -1,34 +0,0 @@
-<?php
- require 'Database.php';
- include 'Components.php';
-
- Database::getInstance('mysql', 'a22willi', 'root', 'Safiren1');
-
- function getForeignKeys($table) {
- // Get the database instance and PDO object
- $db = Database::getInstance();
- $pdo = $db->getPdo();
-
- // Fetch the current database name
- $currentDatabase = $pdo->query('SELECT DATABASE()')->fetchColumn();
-
- // Prepare and execute the statement to fetch foreign keys
- $stmt = $pdo->prepare("
- SELECT COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
- FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
- WHERE TABLE_SCHEMA = :databaseName
- AND TABLE_NAME = :tableName
- AND REFERENCED_TABLE_NAME IS NOT NULL;
- ");
-
- $stmt->bindParam(':databaseName', $currentDatabase);
- $stmt->bindParam(':tableName', $table);
- $stmt->execute();
-
- return $stmt->fetchAll(PDO::FETCH_ASSOC);
- }
-
- foreach (getForeignKeys('Operation') AS $op) {
- print_r($op);
- }
-?>
-\ No newline at end of file