Hi, I'm doing a search in real time in codeigniter, jquery, json and mysql. between the controller and the model I have communication, it brings me the data, but in object. I think the problem is in sending the data to the array in the js, some idea of how to solve it? I have the following code. This is my view:
SEARCH
<div class="formulario">
<label for="caja_busqueda">Buscar</label>
<input type="text" name="caja_busqueda" id="caja_busqueda"></input>
</div>
<div id="datos"></div>
my controller:
function listar() { $salida = "";
if (isset($_POST['consulta'])) {
$query = "SELECT id_persona, nombre_persona,apellido_persona, rut_persona, telefono_persona,
nombre_comuna,edad_persona,nombre_carrera,nombre_ocupacion, fecha_persona FROM persona
JOIN comuna on persona.id_comuna = comuna.id_comuna JOIN ocupacion on persona.id_ocupacion = ocupacion.id_ocupacion
JOIN carrera ON persona.id_carrera = carrera.id_carrera WHERE nombre_persona LIKE '%$q%' OR apellido_persona LIKE '%$q%'
OR rut_persona LIKE '%$q%' OR telefono_persona '%$q%' OR nombre_comuna '%$q%' OR edad_persona '%$q%' OR nombre_carrera '%$q%'
OR nombre_ocupacion LIKE '%$q%' OR fecha_persona LIKE '$q' ";
}
$result = $this->Login_model->getdatos($query);
echo json_encode($result);
if ($result->num_rows>0) {
$salida.="<table border=1 class='tabla_datos'>
<thead>
<tr id='titulo'>
<td>ID</td> <td>NOMBRE</td> <td>APELLIDO</td> <td>RUT</td>
<td>TELEFONO</td> <td>COMUNA</td> <td>EDAD</td>
<td>CARRERA</td> <td>OCUPACION</td> <td>FECHA</td> </tr>
</thead>
<tbody>";
while ($fila = $result->fetch_assoc()) {
$salida.="<tr>
<td>".$fila['id_persona']."</td>
<td>".$fila['nombre_persona']."</td>
<td>".$fila['apellido_persona']."</td>
<td>".$fila['rut_persona']."</td>
<td>".$fila['telefono_persona']."</td>
<td>".$fila['nombre_comuna']."</td>
<td>".$fila['edad_persona']."</td>
<td>".$fila['nombre_carrera']."</td>
<td>".$fila['nombre_ocupacion']."</td>
<td>".$fila['fecha_persona']."</td>
</tr>";
}
$salida.="</tbody></table>";
}else{
$salida.="NO HAY DATOS REGISTROS"; } echo $salida;}
the model:
function getdatos($consulta){
$query=$this->db->query($consulta);
return $query->result();
}
mi js:
$(buscar_datos());
function buscar_datos(consulta){
$.ajax({
url: 'http://localhost/cft/index.php/lista_controller/listar' ,
type: 'POST' ,
dataType: 'json',
data: {consulta: consulta},
})
.done(function(respuesta){
$("#datos").html(respuesta);
})
.fail(function(){
console.log("error");
});
}
$(document).on('keyup','#caja_busqueda', function(){
var valor = $(this).val();
if (valor != "") {
buscar_datos(valor);
}else{
buscar_datos();
}
});