-1

hi all in model i gave in function as follows

$query = $this->db->get();
            $result = $query->result();
            $data[] = $result;

but am getting error as A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: models/survey_model.php

Line Number: 845

null

what was the for this. can someone help me please

function get_actual_details_model($fin_year,$state){
        $this->db->select('*');
        $this->db->from('survey_respondent_info');
        $this->db->where('state', $state);
        $queryYr = $this->db->get();
        $resYr = $queryYr->result();
        foreach ($resYr as  $surveyId) {
            $this->db->select('budgets.*, budget_funding.*, marketing_budget.*, personnel_budget.*, grants_budget.*');
            $this->db->from('budgets');
            $this->db->join('budget_funding', 'budget_funding.budget_id = budgets.budget_id', 'left');
            $this->db->join('marketing_budget', 'marketing_budget.budget_id = budgets.budget_id', 'left');
            $this->db->join('personnel_budget', 'personnel_budget.budget_id = budgets.budget_id', 'left');
            $this->db->join('grants_budget', 'grants_budget.budget_id = budgets.budget_id', 'left');
            $this->db->where('budgets.financial_year',$fin_year);
            $this->db->where('budget_option_id', 1);
            $this->db->where('survey_id', $surveyId->survey_id);
          $data = array();
$query = $this->db->get();
$result = $query->result();
$data[] = $result;
        }
        return $data;
      }
user2412936
  • 349
  • 2
  • 6
  • 16

4 Answers4

2

Example of code you should use. It's only example!

$data = array();
$query = $this->db->get( 'table' );
$result = $query->result();
$data[] = $result;

return $data;

Your code with little update

function get_actual_details_model($fin_year,$state){
    $data = array(); // Data need to be in start
    $this->db->select('*');
    $this->db->from('survey_respondent_info');
    $this->db->where('state', $state);
    $queryYr = $this->db->get();
    $resYr = $queryYr->result();
    foreach ($resYr as  $surveyId) {
        $this->db->select('budgets.*, budget_funding.*, marketing_budget.*, personnel_budget.*, grants_budget.*');
        $this->db->from('budgets');
        $this->db->join('budget_funding', 'budget_funding.budget_id = budgets.budget_id', 'left');
        $this->db->join('marketing_budget', 'marketing_budget.budget_id = budgets.budget_id', 'left');
        $this->db->join('personnel_budget', 'personnel_budget.budget_id = budgets.budget_id', 'left');
        $this->db->join('grants_budget', 'grants_budget.budget_id = budgets.budget_id', 'left');
        $this->db->where('budgets.financial_year',$fin_year);
        $this->db->where('budget_option_id', 1);
        $this->db->where('survey_id', $surveyId->survey_id);
        $query = $this->db->get();
        $result = $query->result();
        $data[] = $result;
    }
    return $data;
}
Arturs
  • 1,258
  • 5
  • 21
  • 28
  • Show your all model, because, if it's same error, then problem is not there. – Arturs Aug 23 '13 at 09:34
  • function get_actual_details_model($fin_year,$state){ $this->db->select('*'); $this->db->from('survey_respondent_info'); $this->db->where('state', $state); $queryYr = $this->db->get(); $resYr = $queryYr->result(); – user2412936 Aug 23 '13 at 09:38
  • foreach ($resYr as $surveyId) { $this->db->select('budgets.*, budget_funding.*, marketing_budget.*, personnel_budget.*, grants_budget.*'); $this->db->from('budgets'); $this->db->join('budget_funding', 'budget_funding.budget_id = budgets.budget_id', 'left'); $this->db->join('marketing_budget', 'marketing_budget.budget_id = budgets.budget_id', 'left'); $this->db->join('personnel_budget', 'personnel_budget.budget_id = budgets.budget_id', 'left'); $this->db->join('grants_budget', 'grants_budget.budget_id = budgets.budget_id', 'left'); – user2412936 Aug 23 '13 at 09:39
  • Just edit your main post and put your code there in `CODE tags` – Arturs Aug 23 '13 at 09:40
  • $this->db->where('budgets.financial_year',$fin_year); $this->db->where('budget_option_id', 1); $this->db->where('survey_id', $surveyId->survey_id); $data = array(); $query = $this->db->get(); $result = $query->result(); $data[] = $result; } return $data; } it is showing as more characters so am sending total code by splitting. – user2412936 Aug 23 '13 at 09:40
  • You need to declare $data in start of function. – Arturs Aug 23 '13 at 09:44
  • if am declaring in the starting am getting empty array as output – user2412936 Aug 23 '13 at 09:47
  • Debug your code. Start with what you get from `$result = $query->result();` in loop. Then debug final `return $data;`. In final, debug your controller, see if there you get information from model. – Arturs Aug 23 '13 at 09:49
  • how to debug to know whether it is getting or not – user2412936 Aug 23 '13 at 09:51
  • For example use command from php. `var_dump` [Manual](http://php.net/manual/en/function.var-dump.php) – Arturs Aug 23 '13 at 09:51
  • i used in this way $query = $this->db->get(); $result = $query->result(); print_r ($query);exit; $data[] = $result; but am not able to see that output – user2412936 Aug 23 '13 at 09:53
  • You store your result in variable $results, but trying to print $query. It's wrong. You need to debug variable where you store your data. – Arturs Aug 23 '13 at 09:56
  • $query = $this->db->get(); $result = $query->result(); print_r ($result);exit; am giving in this way also it is not working arthur – user2412936 Aug 23 '13 at 10:10
  • If You don't get any data from your result, then check your query's. I guess there is the problem, because you don't get result from your DB. – Arturs Aug 23 '13 at 10:12
2

you have to define you $data first.

$data = array();

Change

$data[] = $result;

To

$data = $result;
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
1

Before you put anything in $data, define it as empty array like this:

$data = array();
1

Your $data variable is not defined. And it is appended with $result

Following is the updated code:

$data = array();
$query = $this->db->get();
$result = $query->result();
$data[] = $result;
Pupil
  • 23,834
  • 6
  • 44
  • 66