Quisiera que esta parte del filtro de búsqueda me oriente cual es mi error o la lógica. Ya que eh estado investigando las funciones lógicas del ISSET Y EMPTY Y NOT_NULL con respecto a la lógica de la búsqueda. Ya que busca mis valores que estan en la BD busca correctamente los siguientes valores
Busqueda individual
Modulo
1 y 2 busca los valores y muestra los resultados de la tabla
Mientras del lado test_fuga
0 y 1, el problema de aquí solamente busca el 1 el 0 o valor nulo no busca manda a llamar 1 y 0 ambos.
búsqueda en conjunto (dos)
Al realizar la búsqueda de modulo y test_fuego si busca bien, a excepción del 0 o valor nulo.
Si busca 1 y 1, 2 y 1.
Busqueda no realizada.
1 con 0, 2 con 0.
Esta es la parte de records.php
<?php
include 'model.php';
$model = new Model();
if (isset($_POST['std']) && isset($_POST['res']) && !empty($_POST['std']) && !empty($_POST['res']) ) {
$std = $_POST['std'];
$res = $_POST['res'];
$rows = $model->fetch_filter($std, $res);
}
elseif (isset($_POST['std']) && empty($_POST['res']) ) {
$std = $_POST['std'];
$rows = $model->fetch_std_filter($std);
}
elseif (empty($_POST['std']) && isset($_POST['res'])) {
$res = $_POST['res'];
$rows = $model->fetch_res_filter($res);
}
else {
$rows = $model->fetch();
}
echo json_encode($rows);
De ahi viene la inclusión del model.php
<?php
class Model
{
private $server = "localhost";
private $username = "root";
private $password = "";
private $db = "tecnosistemas";
private $conn;
public function __construct()
{
try {
$this->conn = new mysqli($this->server, $this->username, $this->password, $this->db);
} catch (\Throwable $th) {
//throw $th;
echo "Connection error " . $th->getMessage();
}
}
// Fetch modulo
public function fetch_std()
{
$data = [];
$query = "SELECT DISTINCT `modulo` FROM `proyecto` ORDER BY `modulo` ASC";
if ($sql = $this->conn->query($query)) {
while ($row = mysqli_fetch_assoc($sql)) {
$data[] = $row;
}
}
return $data;
}
// Fetch Test de fuga (res)
public function fetch_res()
{
$data = [];
$query = "SELECT DISTINCT `test_fuga` FROM `proyecto` ORDER BY `test_fuga` ASC";
if ($sql = $this->conn->query($query)) {
while ($row = mysqli_fetch_assoc($sql)) {
$data[] = $row;
}
}
return $data;
}
// Fetch Records
public function fetch()
{
$data = [];
$query = "SELECT * FROM proyecto";
if ($sql = $this->conn->query($query)) {
while ($row = mysqli_fetch_assoc($sql)) {
$data[] = $row;
}
}
return $data;
}
// Filter modulo y test de fuga
public function fetch_filter($std, $res)
{
$data = [];
$query = "SELECT * FROM proyecto WHERE modulo = '$std' AND test_fuga = '$res' ";
if ($sql = $this->conn->query($query)) {
while ($row = mysqli_fetch_assoc($sql)) {
$data[] = $row;
}
}
return $data;
}
// Filter modulo
public function fetch_std_filter($std)
{
$data = [];
$query = "SELECT * FROM proyecto WHERE modulo = '$std'";
if ($sql = $this->conn->query($query)) {
while ($row = mysqli_fetch_assoc($sql)) {
$data[] = $row;
}
}
return $data;
}
// Filter Test de fuga
public function fetch_res_filter($res)
{
$data = [];
$query = "SELECT * FROM proyecto WHERE test_fuga = '$res'";
if ($sql = $this->conn->query($query)) {
while ($row = mysqli_fetch_assoc($sql)) {
$data[] = $row;
}
}
return $data;
}
}
Por ultimo una parte del indexbuscador
<div class="row">
<div class="col-xl-13 mt-5 mb-5">
<!-- Table -->
<div class="table-responsive">
<table class="table table-striped" id="record_table">
<thead class="thead-dark">
<tr>
<th>id</th>
<th>Fecha</th>
<th>Hora</th>
<th>Modulo</th>
<th>Operador</th>
<th>Test camara</th>
<th>Presion cabezal</th>
<th>Estabilizacion</th>
<th>Presion inicio</th>
<th>Presion final</th>
<th>Caida presion</th>
<th>Test fuga</th>
<th>Tiempo ciclo</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.5.0.min.js"
integrity="sha256-xNzN2a4ltkB44Mc/Jz3pT4iU1cmeR0FkXs4pru/JxaQ=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous">
</script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"
integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous">
</script>
<script type="text/javascript" src="https://cdn.datatables.net/v/bs4/dt-1.10.20/r-2.2.3/datatables.min.js"></script>
<!-- Moment Js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script>
// Fetch modulo
function fetch_std() {
$.ajax({
url: "fetch_std.php",
type: "post",
dataType: "json",
success: function(data) {
var stdBody = "";
for (var key in data) {
stdBody += `<option value="${data[key]['modulo']}">${data[key]['modulo']}</option>`;
}
$("#select_std").append(stdBody);
}
});
}
fetch_std();
// Fetch Test de fuga
function fetch_res() {
$.ajax({
url: "fetch_res.php",
type: "post",
dataType: "json",
success: function(data) {
var resBody = "";
for (var key in data) {
resBody += `<option value="${data[key]['test_fuga']}">${data[key]['test_fuga']}</option>`;
}
$("#select_res").append(resBody);
}
});
}
fetch_res();
// Fetch Records
function fetch(std, res) {
$.ajax({
url: "records.php",
type: "post",
data: {
std: std,
res: res,
},
dataType: "json",
success: function(data) {
var i = 1;
$('#record_table').DataTable({
"data": data,
"responsive": true,
"columns": [{
"data": "id",
"render": function(data, type, row, meta) {
return i++;
}
},
{
"data": "fecha",
},
{
"data": "hora",
},
{
"data": "modulo"
},
{
"data": "operador",
},
{
"data": "test_camara",
},
{
"data": "presion_cabezal",
},
{
"data": "estabilizacion",
},
{
"data": "presion_inicio",
},
{
"data": "presion_final",
},
{
"data": "caida_presion",
},
{
"data": "test_fuga"
},
{
"data": "tiempo_ciclo",
}
]
});
}
});
}
fetch();
// Filter
$(document).on("click", "#filter", function(e) {
e.preventDefault();
var std = $("#select_std").val();
var res = $("#select_res").val();
if (std !== "" && res !== "" ) {
$('#record_table').DataTable().destroy();
fetch(std, res);
} else if (std !== "" && res == "") {
$('#record_table').DataTable().destroy();
fetch(std, '');
} else if (std == "" && res !== "") {
$('#record_table').DataTable().destroy();
fetch('', res);
} else {
$('#record_table').DataTable().destroy();
fetch();
}
});
// Reset modulo
$(document).on("click", "#reset_std", function(e) {
e.preventDefault();
$("#select_std").html(`<option value="">Buscar...</option>`);
var res = $("#select_res").val();
if (res == "") {
$('#record_table').DataTable().destroy();
fetch();
fetch_std();
} else {
$('#record_table').DataTable().destroy();
fetch('', res);
fetch_std();
}
});
// Reset test_fuga
$(document).on("click", "#reset_res", function(e) {
e.preventDefault();
$("#select_res").html(`<option value="">Buscar...</option>`);
var std = $("#select_std").val();
if (std == "") {
$('#record_table').DataTable().destroy();
fetch();
fetch_res();
} else {
$('#record_table').DataTable().destroy();
fetch(std, '');
fetch_res();
}
});
// Reset
$(document).on("click", "#reset", function(e) {
e.preventDefault();
$("#select_std").html(`<option value="">Buscar...</option>`);
$("#select_res").html(`<option value="">Buscar...</option>`);
$('#record_table').DataTable().destroy();
fetch();
fetch_std();
fetch_res();
});
</script>
</body>
</html>
De antemano gracias y espero su ayuda. Gracias y saludos comunidad programadora.