From 67417b0bfc5bc5a61391a8d927f2f489b726b4dd Mon Sep 17 00:00:00 2001 From: coolneng Date: Wed, 17 Jun 2020 05:34:36 +0200 Subject: [PATCH] Implement user searching --- src/database.php | 15 +++++++++++++-- src/forms/user_edit_form.php | 5 +++-- src/user.php | 22 +++++++++++++++++++--- src/user_management.php | 12 ++++++++++++ static/style.css | 6 ++++++ 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/database.php b/src/database.php index 3e58d73..dc4e459 100644 --- a/src/database.php +++ b/src/database.php @@ -97,10 +97,21 @@ function listUsers($pdo) { return $result; } -function findUser($pdo, string $id) { +function fetchUserData($pdo, string $id) { $query = "SELECT * FROM usuario WHERE id=?"; $result = $pdo->prepare($query); - $result->execute([$id]); $data = $result->fetch(); + $result->execute([$id]); + $data = $result->fetch(); + return $data; +} + +function findUser($pdo, $input) { + $input = "%$input%"; + $query = "SELECT usuario.nombre, usuario.usuario, rol.nombre, usuario.correo, usuario.id, fecha_baja + FROM usuario, rol WHERE usuario.rol=rol.codigo AND (usuario.nombre LIKE ? OR usuario.usuario LIKE ? OR usuario.correo LIKE ?)"; + $result = $pdo->prepare($query); + $result->execute([$input, $input, $input]); + $data = $result->fetchAll(); return $data; } diff --git a/src/forms/user_edit_form.php b/src/forms/user_edit_form.php index 88263bd..8fd1acb 100644 --- a/src/forms/user_edit_form.php +++ b/src/forms/user_edit_form.php @@ -11,8 +11,9 @@ include '../database.php'; $pdo = connectDatabase(); - $data = findUser($pdo, $_GET["edit"]); - ?>
+ $data = fetchUserData($pdo, $_GET["edit"]); + ?> +
diff --git a/src/user.php b/src/user.php index 8a4ae94..ea26c3e 100644 --- a/src/user.php +++ b/src/user.php @@ -9,6 +9,9 @@ +
+ Crear +
@@ -23,7 +26,13 @@ include 'database.php'; $pdo = connectDatabase(); - $list = listUsers($pdo); + + if (isset($_GET["search"])) { + $list = findUser($pdo, $_GET["search"]); + } else { + $list = listUsers($pdo); + } + foreach($list as $row) : if(!$row[5]): ?> @@ -43,8 +52,15 @@
-
- Crear +
+ +
+ +
+
+ +
+
diff --git a/src/user_management.php b/src/user_management.php index e7df7e9..f478f76 100644 --- a/src/user_management.php +++ b/src/user_management.php @@ -26,6 +26,14 @@ function userDeletion(){ header('location: user.php'); } +function userFind(){ + $data = $_POST; + $pdo = connectDatabase(); + findUser($pdo, $data); + closeDatabase($pdo); + $search = $data["search_box"]; + header("location: user.php?search=$search"); +} if (isset($_POST["create"])) { userCreation(); @@ -39,3 +47,7 @@ if (isset($_POST["edit"])) { if (isset($_GET["delete"])) { userDeletion(); } + +if (isset($_POST["search"])) { + userFind(); +} diff --git a/static/style.css b/static/style.css index b7d88cd..51a528d 100644 --- a/static/style.css +++ b/static/style.css @@ -46,6 +46,12 @@ form { border-radius: 5px; border: 1px solid gray; } + +.search-group input { + height: 30px; + width: 93%; + font-size: 16px; +} .btn { padding: 10px; font-size: 15px;