codeigniter返回从控制器获取数据以通过Ajax请求查看

iaz*_*han 1 php mysql ajax json codeigniter

我加载了我想通过ajax JSON请求从数据库中显示获取记录的视图.但是它没有显示记录.

这是我的观看代码

<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>
Run Code Online (Sandbox Code Playgroud)

这是我的模特

public function get_hodm()
     {
          return $this->db->get("hodm");
     }
Run Code Online (Sandbox Code Playgroud)

这是我的控制器

public function dig_short_hodm_table(){

        $data['result']=$this->pojo->get_hodm();

        return json_encode($data);

     }
Run Code Online (Sandbox Code Playgroud)

当我加载我的页面时,它显示错误

Message: Undefined variable: result
Run Code Online (Sandbox Code Playgroud)

我想在查看加载时从数据库中获取记录并显示在视图表中.

the*_*EUG 5

更新您的型号:

public function get_hodm(){
      return $this->db->get("hodm")->result();
}
Run Code Online (Sandbox Code Playgroud)

你的控制器:

    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);
}
Run Code Online (Sandbox Code Playgroud)

最后你的看法:

<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>
Run Code Online (Sandbox Code Playgroud)