Implement user searching

This commit is contained in:
coolneng 2020-06-17 05:34:36 +02:00
parent e77f29b393
commit 67417b0bfc
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
5 changed files with 53 additions and 7 deletions

View File

@ -97,10 +97,21 @@ function listUsers($pdo) {
return $result; return $result;
} }
function findUser($pdo, string $id) { function fetchUserData($pdo, string $id) {
$query = "SELECT * FROM usuario WHERE id=?"; $query = "SELECT * FROM usuario WHERE id=?";
$result = $pdo->prepare($query); $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; return $data;
} }

View File

@ -11,8 +11,9 @@
include '../database.php'; include '../database.php';
$pdo = connectDatabase(); $pdo = connectDatabase();
$data = findUser($pdo, $_GET["edit"]); $data = fetchUserData($pdo, $_GET["edit"]);
?> <form method="post" action="../user_management.php"> ?>
<form method="post" action="../user_management.php">
<div class="input-group"> <div class="input-group">
<label>Nombre</label> <label>Nombre</label>
<input type="text" name="nombre" value="<?php echo $data[1]; ?>"> <input type="text" name="nombre" value="<?php echo $data[1]; ?>">

View File

@ -9,6 +9,9 @@
<link rel="stylesheet" href="../static/style.css" type="text/css" media="screen" /> <link rel="stylesheet" href="../static/style.css" type="text/css" media="screen" />
</head> </head>
<body> <body>
<div style="text-align: right; margin-top: 20px;">
<a href="forms/user_create_form.html" class="create_btn" >Crear</a>
</div>
<table> <table>
<thead> <thead>
<tr> <tr>
@ -23,7 +26,13 @@
include 'database.php'; include 'database.php';
$pdo = connectDatabase(); $pdo = connectDatabase();
if (isset($_GET["search"])) {
$list = findUser($pdo, $_GET["search"]);
} else {
$list = listUsers($pdo); $list = listUsers($pdo);
}
foreach($list as $row) : foreach($list as $row) :
if(!$row[5]): if(!$row[5]):
?> ?>
@ -43,8 +52,15 @@
<?php endforeach ?> <?php endforeach ?>
<?php closeDatabase($pdo); ?> <?php closeDatabase($pdo); ?>
</table> </table>
<div style="text-align: right;"> <div>
<a href="forms/user_create_form.html" class="create_btn" >Crear</a> <form method="post" action="user_management.php">
<div class="search-group">
<input type="text" name="search_box" value="">
</div>
<div class="input-group">
<button class="btn" type="submit" name="search" >Buscar</button>
</div>
</form>
</div> </div>
</body> </body>
</html> </html>

View File

@ -26,6 +26,14 @@ function userDeletion(){
header('location: user.php'); 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"])) { if (isset($_POST["create"])) {
userCreation(); userCreation();
@ -39,3 +47,7 @@ if (isset($_POST["edit"])) {
if (isset($_GET["delete"])) { if (isset($_GET["delete"])) {
userDeletion(); userDeletion();
} }
if (isset($_POST["search"])) {
userFind();
}

View File

@ -46,6 +46,12 @@ form {
border-radius: 5px; border-radius: 5px;
border: 1px solid gray; border: 1px solid gray;
} }
.search-group input {
height: 30px;
width: 93%;
font-size: 16px;
}
.btn { .btn {
padding: 10px; padding: 10px;
font-size: 15px; font-size: 15px;