"Message: Undefined Variable: Query" error when joining two tables codeigniter mysql php

I have two tables named "addexpense" and "addcategory". I have successfully joined each of the tables, but no data is being viewed on my view page 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 }?>

      

+3


source to share


5 answers


set the controller function this way



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

      

+1


source


Please, you can check if your data is in the browse file or not by typing it at the beginning of your browse.



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

      

0


source


It seems that you haven't initialized model

in controller

.

As controller

you first need to download model

before you use it properties

. Please check below 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 it doesn't work.

0


source


Result of changing model () in 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 will try below code

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

}

      

Make sure it's 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>

      

0


source


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

      

To 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 if the model is sending data or not and please update it.

0


source







All Articles