将变量从控制器传递到视图

时间:2016-07-11 18:35:44

标签: php codeigniter

在我的控制器中,我可以获取组织名称,但是当我将其传递给视图时 那是一个错误。它说为foreach()提供了无效的参数:

Error Message

这是我的代码。

控制器

public function index()

{
  $user_id = $this->session->userdata('user_id');
  $data['title'] = "User";
  $getID['orgID'] = $this->userModel->getOrganizationID($user_id); // used my session user_id to 
    foreach ($getID['orgID'] as $orgID) 
  {
    $org_id = $orgID->org_id;

    $getName['myOrganization'] = $this->userModel->myOrganization($org_id);

    foreach($getName['myOrganization'] as $orgName)
    {
     $name = $orgName->org_name;
     $data['name'] = $name;
    }
  }
  $this->load->view('xxxx/xxxx/xxxx',$data);

模型

public function getOrganizationID($user_id)
    {
        $this->db->select('org_id');
        $this->db->from('organization_members');
        $this->db->where('user_id', $user_id);
        $query = $this->db->get(); 
        return $query->result();

    }
    public function myOrganization($org_id)
    {
        $this->db->select('org_name');
        $this->db->from('tblorganization');
        $this->db->where('org_id', $org_id);
        $query = $this->db->get(); 
        return $query->result();

    }

My output

第一个数组是$ getID的结果[' orgID'] = $ this-> userModel-> getOrganizationID($ user_id);我使用我的user_id会话来获取用户的所有org_id

第二个数组是$ getName的结果[' myOrganization'] = $ this-> userModel-> myOrganization($ org_id);我使用我的org_id(来自我以前的方法)来获取用户的所有org_name。

2 个答案:

答案 0 :(得分:0)

Is there going to be more then one result? Because if its only one result then you can use $query->row(); and eliminate the foreach completely.

Always check to make sure your database method worked AND that you actually got a returned value whenever you are making any database call. So i'll let you add the if condition in the database method but in short it should return FALSE if nothing came back. So thats the database method heres one way of doing it in your controller. Note this: $getID['orgID'] is very awkward. You are getting results back from the members table so call it members.

// check for the negative first - if no members came back 
if( ! $members = $this->userModel->getOrganizationID($user_id) ) 
{
    // if no results back leave this method 
    // pass the user id so you can echo it out in the error page 
    $this->showNoResultsFor($user_id) ; 
} 
else{

    foreach ($members as $member) 
  {

    $org_id = $member->org_id;

     // etc etc etc 

答案 1 :(得分:0)

I'm not a codeigniter expert but looking at your code, I am wondering why you are setting:

$getID['orgID'] = $this->userModel->getOrganizationID($user_id);

First, you are setting an array $getID['orgID'] rather than just using something like $memberships = ...; I'm not sure why you are casting an array.

Secondly, you seem to be referencing a model class without instantiating it:

$this->userModel->getOrganizationID($user_id);

Perhaps codeigniter does some magic? $this refers to this instance and from the code you show, your model is likely in a separate class/file so I am unclear how $this->userModel is referenced in your method, unless you are instantiating it in your Controller's constructor?

From what I see it looks like you are getting the error because you are not supplying a valid object/array to your foreach. Perhaps start by testing you are actually getting a valid return from $this->userModel->getOrganizationID($user_id).