2

我被这个愚蠢的问题困住了好几次,我知道我可能犯了一些愚蠢的错误,基本上我有一个控制器,它调用模型函数并返回数据,然后将它分配到一个数组中,但我不断接到电话对非对象成员错误的成员函数 num_rows,

我的控制器功能是:

public function registerNewLease()
{
    $this->load->model('AnnualBudget');
    $alreadyExist = $this->AnnualBudget->checkAlreadyExist($client_block_ID);
    if ($alreadyExist == FALSE) 
    {
      // code if nothing found
    } else 
    {
     for ($i=0; $i < $alreadyExist->num_rows(); $i++) 
     { 
       $row = $alreadyExist->row($i);
       $FY_budget_ID[$i] = $row->FY_budget_ID;
     }
    }
}

My model is :

public function checkAlreadyExist($client_block_ID)
{
    $this->db->select('FY_budget_ID');
    $this->db->where('client_block_ID', $client_block_ID);
    $query = $this->db->get('fy_budget');
    if ($query->num_rows() >= 1) 
    {
        return $query;
    } else 
    {
        return FALSE;
    }

}

错误出现在调用 num_rows 函数的 else 部分...

4

3 回答 3

0

如果您陷入for循环,请尝试使用foreach. 这是我更新的答案:

public function registerNewLease()
{
        $this->load->model('AnnualBudget');
        $alreadyExist = $this->AnnualBudget->checkAlreadyExist($client_block_ID);
        if ($alreadyExist != NULL){
            $i = 0;
            foreach ($alreadyExist->result_array() as $row){
            $i++;
            $FY_budget_ID[$i] = $row['FY_budget_ID'];
            }
        }
        else{
    //no result here
         echo "no result";
        }
}

和模型:

function checkAlreadyExist($client_block_ID)
{
     return $this->db->get_where('fy_budget','client_block_ID' => $client_block_ID);
}
于 2013-10-24T07:09:00.057 回答
0

尝试这个:

public function checkAlreadyExist($client_block_ID)
{
    $data   = array();
    $this->db->select('FY_budget_ID');
    $this->db->where('client_block_ID', $client_block_ID);
    $data   = $this->db->get('fy_budget')->result_array();
    return $data;
}

public function registerNewLease()
{
    $this->load->model('AnnualBudget');
    $FY_budget_ID = array();
    $alreadyExist = $this->AnnualBudget->checkAlreadyExist($client_block_ID);
    echo $this->db->last_query();
    /*if( isset( $alreadyExist ) && count( $alreadyExist ) > 0 ){
        foreach ( $alreadyExist as $key => $each ) 
        { 
            $FY_budget_ID[$key] = $each['FY_budget_ID'];
        }    
    }*/
    echo "<pre>";print_r( $alreadyExist );
}
于 2013-10-24T07:11:41.367 回答
0

使用以下

public function registerNewLease()
{
    $this->load->model('AnnualBudget');
    $alreadyExist = $this->AnnualBudget->checkAlreadyExist($client_block_ID);
    if ($alreadyExist == FALSE) 
    {
      // code if nothing found
    } else 
    {
     for ($i=0; $i < $this->db->count_all_results(); $i++) 
     { 
       $row = $alreadyExist->row($i);
       $FY_budget_ID[$i] = $row->FY_budget_ID;
     }
    }
}

My model is :

public function checkAlreadyExist($client_block_ID)
{
    $this->db->select('FY_budget_ID');
    $this->db->where('client_block_ID', $client_block_ID);
    $query = $this->db->get('fy_budget');
    if ($this->db->count_all_results() >= 1) 
    {
        return $query;
    } else 
    {
        return FALSE;
    }

}
于 2013-10-24T07:18:13.993 回答