简体   繁体   English

从控制器传递变量到视图

[英]Pass variable from controller to view

In my controller, I can get the organization name but when I pass it to the view there's an error. 在我的控制器中,我可以获得组织名称,但是当我将其传递给视图时,会出现错误。 It said invalid argument supplied for foreach( ): 它说为foreach()提供了无效的参数:

错误信息 .

This is my codes. 这是我的代码。

Controller 控制者

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

Model 模型

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 我的输出

First array is my result of $getID['orgID'] = $this->userModel->getOrganizationID($user_id); 第一个数组是我的$ getID ['orgID'] = $ this-> userModel-> getOrganizationID($ user_id);的结果; which I used my user_id session to get all the org_id of the user then 我使用user_id会话来获取用户的所有org_id,然后

Second array is my result of $getName['myOrganization'] = $this->userModel->myOrganization($org_id); 第二个数组是$ getName ['myOrganization'] = $ this-> userModel-> myOrganization($ org_id);的结果。 which I used my org_id(from my previous method) to get all the org_name of the user. 我使用我的org_id(来自先前的方法)来获取用户的所有org_name。

Is there going to be more then one result? 会有一个以上的结果吗? Because if its only one result then you can use $query->row(); 因为如果只有一个结果,则可以使用$ query-> row();。 and eliminate the foreach completely. 并彻底消除foreach。

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. 因此,我将让您在数据库方法中添加if条件,但简而言之,如果什么也没回来,它应该返回FALSE。 So thats the database method heres one way of doing it in your controller. 因此,这就是数据库方法,这是在控制器中执行该方法的一种方法。 Note this: $getID['orgID'] is very awkward. 请注意:$ getID ['orgID']非常尴尬。 You are getting results back from the members table so call it members. 您正在从members表中获取结果,因此将其称为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 

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 = ... ; 首先,您要设置一个数组$getID['orgID']而不仅仅是使用$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? 也许codeigniter做一些魔术? $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? $this指的是这个实例,根据您显示的代码,您的模型可能位于单独的类/文件中,因此,我不清楚如何在您的方法中引用$this->userModel ,除非您在Controller的构造函数中对其进行实例化?

From what I see it looks like you are getting the error because you are not supplying a valid object/array to your foreach. 从我看来,您似乎收到了错误,因为您没有为foreach提供有效的对象/数组。 Perhaps start by testing you are actually getting a valid return from $this->userModel->getOrganizationID($user_id) . 也许从测试开始,您实际上是从$this->userModel->getOrganizationID($user_id)获得有效的回报。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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