简体   繁体   中英

error message in php in codeigniter, Undefined variable: data

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

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

you have to define you $data first.

$data = array();

Change

$data[] = $result;

To

$data = $result;

在将任何数据放入$ data之前,将其定义为空数组,如下所示:

$data = array();

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM