commit 9f91e82ed3123671a556c9c7304e52794d6e23d5
parent 8e0b88c90e29553889f1880efe1686c889ea39aa
Author: William Lindholm <william_lindholm@outlook.com>
Date: Wed, 27 Sep 2023 18:17:49 +0000
Added CreateTableWithRedirect() function.
Diffstat:
1 file changed, 51 insertions(+), 4 deletions(-)
diff --git a/tableFactory.php b/tableFactory.php
@@ -47,15 +47,61 @@
* Clicking "Operation1" would result in a redirect to "./operationdetails.php/?OperationName=Operation1"
*/
//
- public static function createTableWithRedirect($tableName, $adress, $queryColumns) {
+ public static function createTableWithRedirect($tableName, $address, $queryColumns) {
$db = dbconnection::getInstance();
$pdo = $db->getPdo();
+
+ $output = "<div class='overflow-auto'><table class='table table-striped table-bordered table-hover'>";
+
+ try {
+ $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;
+ }
+
+ $queryParams = [];
+ foreach ($queryColumns as $col) {
+ $queryParams[] = $col . "=" . urlencode($row[$col]);
+ }
+ $redirectUrl = $address . "?" . implode("&", $queryParams);
+
+ $output .= "<tr onclick=\"window.location.href = '" . $redirectUrl . "'\" style='cursor: pointer;'>";
+ foreach($row as $key => $value) {
+ if(!is_numeric($key)) {
+ $output .= "<td>" . $value . "</td>";
+ }
+ }
+ $output .= "</tr>";
+ }
+ $output .= "</tbody></table></div>";
+
+ return $output;
+ } catch (PDOException $e) {
+ echo "Error: " . $e->getMessage();
+ return '';
+ }
+ }
+ /**
+ * Create a custom table, that displays any table generated from an sql query.
+ */
+ public static function createCustomTable($query) {
+ $db = dbconnection::getInstance();
+ $pdo = $db->getPdo();
+
$output = "<div class='overflow-auto'><table class='table table-striped table-bordered'>";
-
+
try {
$firstRow = true;
- foreach($pdo->query('SELECT * FROM ' . $tableName . ';') AS $row) {
+ foreach($pdo->query($query) AS $row) {
if($firstRow) {
$output .= "<thead class='thead-dark'><tr>";
foreach($row as $key => $value) {
@@ -72,9 +118,10 @@
$output .= "<td>" . $value . "</td>";
}
}
+ $output .= "</tr>";
}
$output .= "</tbody></table></div>";
-
+
return $output;
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();