2

I load the view in Which I want to display fetch record from the database through the ajax JSON request.But it's not showing the record.

Here is my view code

<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>

Here is my model

public function get_hodm()
     {
          return $this->db->get("hodm");
     }

Here is my controller

public function dig_short_hodm_table(){

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

        return json_encode($data);

     }

When I load my page then it showing the error

Message: Undefined variable: result

I want to when view load it fetch the record from the database and show in the view tables.

2
  • load a view page in controller function @xr33dx Commented Apr 11, 2017 at 7:20
  • @Parvez its not working Commented Apr 11, 2017 at 7:24

5 Answers 5

6

Update your model:

public function get_hodm(){
      return $this->db->get("hodm")->result();
}

Your controller:

    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);
}

Finally your view:

<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>
Sign up to request clarification or add additional context in comments.

10 Comments

Can you give me a descriptive error message that you are getting?
A PHP Error was encountered Severity: Notice Message: Undefined variable: result Filename: digital/dashboard.php Line Number: 38 Backtrace: File: C:\xampp\htdocs\tmt_project\application\views\digital\dashboard.php Line: 38 Function: _error_handler File: C:\xampp\htdocs\tmt_project\application\controllers\digital\dashboard.php Line: 17 Function: view File: C:\xampp\htdocs\tmt_project\index.php Line: 292 Function: require_once
I'll add another answer give me a few seconds
Ok, Thank you. I am waiting
Please click up to my question . I already mark your answer.. Thanx for help
|
0

you must change like this

public function get_hodm()
 {
      return $this->db->get("hodm")->result();
 }

13 Comments

tell me what the problem or message?
i know the problem you just change $data['result'] with $data
first this message Message: Undefined variable: result blink then it show blank page
oh change this line to return json_encode($data); into echo json_encode($data);
what do you change and what the message, and do you know console? to detect your fault?
|
0

I have update Model View and 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>

Here is my model

public function get_hodm()
{
    $query = $this->db->get("hodm");
    return $query->result_array();
}

Here is my controller

public function dig_short_hodm_table(){

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

    echo json_encode($data);

}

2 Comments

Can you check in console? what response are you getting
I got the answer thanks for help. I prefer theEUG answer
0

try to return in controller like this by changing header content

public function dig_short_hodm_table(){
header('Content-Type: application/x-json; charset=utf-8');
$data['result']=$this->pojo->get_hodm();
echo(json_encode($data));
}

Comments

0

Change Your Model Like Below

public function get_hodm()
{
    $query = $this->db->get("hodm");
    return $query -> result();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.