Cargo la vista en la que quiero mostrar el registro de recuperación de la base de datos a través de la solicitud ajax JSON. Pero no muestra el registro.
Aquí está mi código de vista
<div class="col-md-6" id="hodm_table"> <table class="table table-striped table-hover table-responsive"> <thead> <tr> <th>Task Name</th> <th>Director</th> <th>Duration</th> <th>Status</th> </tr> </thead> <tbody> <?php foreach($result as $hodm) { ?> <tr> <td><?php echo $hodm->t_name;?></td> <td><?php echo $hodm->director;?></td> <td><?php echo $hodm->duration;?></td> <td><?php echo $hodm->status;?></td> <?php } ?> </tbody> </table> </div> </div> <script type='text/javascript' language='javascript'> $(document).ready(function(){ $.ajax({ url:"<?php echo base_url();?>digital/dashboard/dig_short_hodm_table", type: 'POST', dataType: 'JSON', success:function (data) { $('#hodm_table').html(data); } }); event.preventDefault(); }); </script>Aquí está mi modelo
public function get_hodm() { return $this->db->get("hodm"); }Aquí está mi controlador
public function dig_short_hodm_table(){ $data['result']=$this->pojo->get_hodm(); return json_encode($data); }Cuando cargo mi página, muestra el error.
Message: Undefined variable: resultCuando la vista lo cargue, quiero obtener el registro de la base de datos y mostrarlo en las tablas de vista.
Actualiza tu modelo:
public function get_hodm(){ return $this->db->get("hodm")->result(); }Tu controlador:
public function dig_short_hodm_table(){ $result_html = ''; $result_set = $this->pojo->get_hodm(); foreach($result_set as $result) { $result_html .= ' <tr> <td>' . $result->t_name . '</td> <td>' . $result->director . '</td> <td>' . $result->duration . '</td> <td>' . $result->status . '</td> </tr>'; } echo json_encode($result_html); }Finalmente tu opinión:
<div class="col-md-6" id="hodm_table"> <table class="table table-striped table-hover table-responsive"> <thead> <tr> <th>Task Name</th> <th>Director</th> <th>Duration</th> <th>Status</th> </tr> </thead> <tbody id="hodm_results"> </tbody> </table> </div> <script type='text/javascript' language='javascript'> $(document).ready(function(){ $.ajax({ url:"<?php echo base_url();?>digital/dashboard/dig_short_hodm_table", type: 'POST', dataType: 'JSON', success:function (data) { $('#hodm_results').html(data); } }); event.preventDefault(); }); </script>debes cambiar asi
public function get_hodm() { return $this->db->get("hodm")->result(); }He actualizado Model View y Controller:
<script type='text/javascript' language='javascript'> $(document).ready(function(){ $.ajax({ url:"<?php echo base_url();?>digital/dashboard/dig_short_hodm_table", type: 'POST', dataType: 'JSON', success:function (data) { var result = data.result;' var row = ""; for(i=0; i < result.length; i++) { row += "<tr>"; row += "<td>"+result[i].t_name+"</td>"; row += "<td>"+result[i].director+"</td>"; row += "<td>"+result[i].duration+"</td>"; row += "<td>"+result[i].status+"</td>"; row += "</tr>"; } $('#hodm_table > tbody').html(row); } }); event.preventDefault(); }); </script>Aquí está mi modelo
public function get_hodm() { $query = $this->db->get("hodm"); return $query->result_array(); }Aquí está mi controlador
public function dig_short_hodm_table(){ $data['result']=$this->pojo->get_hodm(); echo json_encode($data); }