“消息:未定义变量:查询”错误加入两个表codeigniter mysql php

I have two tables named 'addexpense' and 'addcategory' . I have successfully joined each of the tables but on my view page the data are not viewing and an error message is passed on the screen. Please help.

This is my model

public function getExpenses(){
 $this->db->select("addexpense.exp_date, addexpense.exp_amount, addexpense.exp_note, addexpense.exp_created, addcategory.category_name");
 $this->db->from('addexpense');
 $this->db->join('addcategory', 'addcategory.category_id = addexpense.category_id');
 $query = $this->db->get();
 return $query->result();
}

This is my Controller

public function join(){
  $query = $this->base_model->getExpenses();
  //$data['result'] = null;
    if($query)
    {
        $data['query'] =  $query;
    }
  $this->load->view('listexpense', $data);
}

This is my view code

<tr>
    <td><strong>Date</strong></td>
    <td><strong>Amount</strong></td>
    <td><strong>Note</strong></td>
    <td><strong>Created</strong></td>
    <td><strong>Category Name</strong></td>
</tr> 
<?php foreach($query as $expenses){?>
<tr>
    <td><?=$expenses->exp_date;?></td>
    <td><?=$expenses->exp_amount;?></td>
    <td><?=$expenses->exp_note;?></td>
    <td><?=$expenses->exp_created;?></td>
    <td><?=$expenses->category_name;?></td>
</tr>     
<?php }?>

set controller function this way

   public function join(){
      $data['query'] = $this->base_model->getExpenses();
      $this->load->view('listexpense', $data);
    }

Please can you check your data is going on view file or not by printing it at start of your view

<?php echo '<pre>';
 print_r($query);
 ?>

It seems like you have not initialized model in controller.

In your controller you need to load model first before using its properties. Please check below for your controller method.

public function join() {
    $this->load->model('base_model');
    $query = $this->base_model->getExpenses();
    $data = array();
    if ($query) {
        $data['query'] = $query;
    }
    $this->load->view('listexpense', $data);
}

Let me know still it not works.

Model change result() to result_array()

public function getExpenses(){
    $this->db->select("addexpense.exp_date, addexpense.exp_amount, addexpense.exp_note, addexpense.exp_created, addcategory.category_name");
    $this->db->from('addexpense');
    $this->db->join('addcategory', 'addcategory.category_id = addexpense.category_id');
    $query = $this->db->get();
    return $query->result_array();
}

Controller try code below

public function join()
{
    $results = $this->base_model->getExpenses();

    $data['expenses'] = array();

    foreach ($results as $result) {
        $data['expenses'][] = array(
            'exp_date' => $result['exp_date'],
            'exp_amount' => $result['exp_amount'],
            'exp_note' => $result['exp_note'],
            'exp_created' => $result['exp_created'],
            'category_name' => $result['category_name']
        );
    }

    $this->load->view('listexpense', $data);

}

View make sure is the correct view

<table>
<thead>
</thead>
<tbody>
<?php foreach($expenses as $expense){?>
<tr>
    <td><?php echo $expense['exp_date'];?></td>
    <td><?php echo $expense['exp_amount'];?></td>
    <td><?php echo $expense['exp_note'];?></td>
    <td><?php echo $expense['exp_created'];?></td>
    <td><?php echo $expense['category_name'];?></td>
</tr>     
<?php }?>
</tbody>
</table>

Here is the code please check and update Model

public function getExpenses(){
   $this->db->select("addexpense.exp_date, addexpense.exp_amount, addexpense.exp_note, addexpense.exp_created, addcategory.category_name");
   $this->db->from('addexpense');
   $this->db->join('addcategory', 'addcategory.category_id = addexpense.category_id');
   $query = $this->db->get();
   $query_rows = $query->num_rows();
   if($query_rows > 0){
          return $query->result();
   } else{
          return 0;
   }
}

For Controller

public function join(){
    $this->load->model('base_model');
    $query = $this->base_model->getExpenses();
    //print_r($query);die();
    $data['query'] = '';
    if($query != 0){
            $data['query'] = $query;
    }

    $this->load->view('listexpense', $data);
}

For View

<tr>
    <td><strong>Date</strong></td>
    <td><strong>Amount</strong></td>
    <td><strong>Note</strong></td>
    <td><strong>Created</strong></td>
    <td><strong>Category Name</strong></td>
</tr> 
<?php
    if($query != ''){ 
    foreach($query as $expenses){?>
        <tr>
        <td><?=$expenses->exp_date;?></td>
        <td><?=$expenses->exp_amount;?></td>
        <td><?=$expenses->exp_note;?></td>
        <td><?=$expenses->exp_created;?></td>
        <td><?=$expenses->category_name;?></td>
    </tr>     
<?php } }?>

remove the comment "//print_r($query);die();" and check that whether the model is sending the data or not and please update..