Mysql query with if - else condition
Mysql query with if - else condition
Good day I need to get the debtors of a community, to log in to the community you can be a superadmin, who can see all the records, but if you log in as an administrator of a community, you can only see the records of that community. I have the following sql:
`public function getReservas(){
// Obtenemos el ID de la comunidad del usuario actual desde la sesión
// $id_comunidad = $_SESSION['id_comunidad'];
// Verificamos si el usuario es superadmin
$superadmin = $_SESSION['role_id'] == 1;
// Si es superadmin, mostramos todas las dependencias
if ($superadmin) {
$sql = "SELECT r.id_reserva, r.id_depto, r.id_lugar, r.fechaHoraInicio, r.fechaHoraTermino, r.valor, r.nota, r.estado, r.id_comunidad,
d.id_depto, d.nro_depto, l.id_lugar, l.lugar
FROM reservas r
LEFT JOIN deptos d ON r.id_depto = d.id_depto
LEFT JOIN lugares l ON r.id_lugar = l.id_lugar";
} else {
// Si no es superadmin, solo mostramos las reservas de su comunidad
$sql = "SELECT r.id_reserva, r.id_depto, r.id_lugar, r.fechaHoraInicio, r.fechaHoraTermino, r.valor, r.nota, r.estado, r.id_comunidad,
d.id_depto, d.nro_depto
FROM reservas r
LEFT JOIN deptos d ON r.id_depto = d.id_depto
LEFT JOIN lugares l ON r.id_lugar = l.id_lugar
WHERE r.id_comunidad = 1";
}
// Ejecutamos la consulta
$data = $this->selectAll($sql);
// Devolvemos los datos
return $data;
}
`
The first part of the ife works, it shows all the records in the view.
Else does not work when calling view gives dataTable error:
DataTables warning: table id=tblReservas - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
What I find strange, since the first part works, it means that JSON is fine, and then why not the second part.
tblReservas = $('#tblReservas').DataTable({
ajax: {
url: base_url + "/Reservas/listar",
dataSrc: ''
},
'columnDefs': [
{className: "dt-body-right", "targets": [5]},
],
columns: [
{ 'data': 'id_reserva' },
{ 'data': 'nro_depto' },
{ 'data': 'lugar' },
{ 'data': 'fechaHoraInicio' },
{ 'data': 'fechaHoraTermino' },
{ 'data': 'valor', render: DataTable.render.number( null, null, 0, '$') },
{ 'data': 'estado' },
{ 'data': 'acciones' },
],
language: {
"url": "//cdn.datatables.net/plug-ins/1.10.11/i18n/Spanish.json"
},
dom,
buttons
});
Replies
The place to start is by following the troubleshooting steps at the link in the error:
http://datatables.net/tn/1
Let us know what you find.
Kevin
The thing is that I am using that code, and I don't see what could be wrong with it, because if I log in as superadmin, it works.
That same json code if I enter as an admin does not work. So that makes me think that that is not the problem,
Its hard to say what the problem might be by looking at the code. It will need debugging. Don't assume something works, verify that it works or not. As I said the first steps is to follow the steps at the link in the error:
http://datatables.net/tn/1
Let us know what you find. If not post a link to your page or a test case replicating the issue so we can help debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Solved
It might help others to know what you did to solve the problem. Please share.
Kevin