public function Listar() { try { /* Anex Grid */ $anexgrid = new AnexGrid(); /* Los registros */ $sql = "\n SELECT * FROM alumnos\n ORDER BY {$anexgrid->columna} {$anexgrid->columna_orden}\n LIMIT {$anexgrid->pagina}, {$anexgrid->limite}\n "; $stm = $this->pdo->prepare($sql); $stm->execute(); $result = $stm->fetchAll(PDO::FETCH_OBJ); /* El total de registros */ $total = $this->pdo->query("\n SELECT COUNT(*) Total\n FROM alumnos\n ")->fetchObject()->Total; return $anexgrid->responde($result, $total); } catch (Exception $e) { die($e->getMessage()); } }
public function Listar() { try { /* Instanciamos AnexGRID */ $anexGrid = new AnexGrid(); /* Contamos los registros*/ $total = $this->pdo->query("\n SELECT COUNT(*) Total\n FROM comprobante\n ")->fetchObject()->Total; /* Nuestra consulta dinámica */ $registros = $this->pdo->query("\n SELECT * FROM comprobante\n ORDER BY {$anexGrid->columna} {$anexGrid->columna_orden}\n LIMIT {$anexGrid->pagina},{$anexGrid->limite}")->fetchAll(PDO::FETCH_ASSOC); foreach ($registros as $k => $r) { /* Traemos los clientes que tiene asignado cada comprobante */ $cliente = $this->pdo->query("SELECT * FROM cliente c WHERE c.id = " . $r['Cliente_id'])->fetch(PDO::FETCH_ASSOC); $registros[$k]['Cliente'] = $cliente; /* Traemos el detalle */ $registros[$k]['Detalle'][] = $this->pdo->query("SELECT * FROM comprobante_detalle cd WHERE cd.Comprobante_id = " . $r['id'])->fetch(PDO::FETCH_ASSOC); foreach ($registros[$k]['Detalle'] as $k1 => $d) { $registros[$k]['Detalle'][$k1]['Producto'] = $this->pdo->query("SELECT * FROM producto p WHERE p.id = " . $d['Producto_id'])->fetch(PDO::FETCH_ASSOC); } } return $anexGrid->responde($registros, $total); } catch (Exception $e) { die($e->getMessage()); } }
<?php require_once 'anexgrid.php'; try { $anexGrid = new AnexGrid(); /* Si es que hay filtro, tenemos que crear un WHERE dinámico */ $wh = "id > 0"; foreach ($anexGrid->filtros as $f) { if ($f['columna'] == 'Nombre') { $wh .= " AND CONCAT(Nombre, ' ', Apellido) LIKE '%" . addslashes($f['valor']) . "%'"; } if ($f['columna'] == 'Correo') { $wh .= " AND Correo LIKE '%" . addslashes($f['valor']) . "%'"; } if ($f['columna'] == 'Sexo' && $f['valor'] != '') { $wh .= " AND Sexo = '" . addslashes($f['valor']) . "'"; } if ($f['columna'] == 'Profesion_id' && $f['valor'] != '') { $wh .= " AND Profesion_id = '" . addslashes($f['valor']) . "'"; } } /* Nos conectamos a la base de datos */ $db = new PDO("mysql:dbname=test;host=localhost;charset=utf8", "root", ""); /* Nuestra consulta dinámica */ $registros = $db->query("\n SELECT * FROM empleado\n WHERE {$wh} ORDER BY {$anexGrid->columna} {$anexGrid->columna_orden}\n LIMIT {$anexGrid->pagina},{$anexGrid->limite}")->fetchAll(PDO::FETCH_ASSOC); $total = $db->query("\n SELECT COUNT(*) Total\n FROM empleado\n WHERE {$wh}\n ")->fetchObject()->Total; foreach ($registros as $k => $r) { $profesion = $db->query("SELECT * FROM profesion p WHERE p.id = " . $r['Profesion_id'])->fetch(PDO::FETCH_ASSOC); $registros[$k]['Profesion'] = $profesion; } header('Content-type: application/json');