代码之家  ›  专栏  ›  技术社区  ›  Sudhi Sr

如何解决消息:尝试获取Codeigniter中非对象错误的属性?

  •  3
  • Sudhi Sr  · 技术社区  · 7 年前

    我在CodeIgniter和练习方面非常新鲜。我现在正在开发一个简单的Codeigniter应用程序,只是为了练习。数据库中有银行及其分行。我只想向分行展示其银行名称。分支机构正在显示,但在控制器中获取银行时,显示此错误。我试了这么多 links ,但什么都没找到。

    "A PHP Error was encountered
    
    Severity: Notice
    
    Message: Trying to get property of non-object"
    
    in Line Number: 85 and 91
    

    这是我的控制器

    function ShowBranchList() {
    
                $branch_list = $this->FD_Model->get_all_branches();
                $get_bank =   $this->FD_Model->get_bank_by_branch($branch_list->bankid); //This is Line Number 85
                if($branch_list ){
                $data = array(
                    'pagetitle' => 'Branch List View',
                    'branch_list_data' => $branch_list,
                    'br_bank' => $get_bank['bank_name'],//This is Line Number 93
                );
    
            }
    

    模型

    /*function for getting all branches*/
    
    function get_all_branches() {
        $this->db->order_by( $this->brid, $this->order );
        return $this->db->get( $this->brtable )->result();
    }
    
    
    /*function for getting banks by branches*/
    
    function get_bank_by_branch( $id ) {
            $this->db->where( $this->bid, $id);
            return $this->db->get( $this->banktable )->row();
        }
    

    最后,这是我的 看法

     foreach ($branch_list_data as $branch_list) 
            { 
              <?php echo $branch_list->brname ?>
              <?php echo $br_bank; ?>
            }
    

    有人知道这个问题吗?

    使现代化

    当我尝试查看查询时

    echo $this->db->last_query();
    

    后果

    选择*自 tbl_bankmaster 在哪里 bid 为空

    价值观 $branch_list->bankid null 。当我对一些值进行硬编码时,视图页面显示时不会出现任何错误。

    4 回复  |  直到 7 年前
        1
  •  1
  •   Abdulla Nilam    7 年前

    像这样做

    $get_bank =   $this->FD_Model->get_bank_by_branch($branch_list[0]->bankid);
    

    以及

    'br_bank' => $get_bank[0]->bank_name # this will work maybe
    

    'br_bank' => $get_bank[0]['bank_name']
    

    它的 0 索引数组,因此您需要添加密钥来访问子数组的

        2
  •  0
  •   Danish Ali    7 年前

    像这样更改视图中的数据

    foreach ($branch_list_data as $branch_list) 
    { 
       echo $branch_list['brname']; //change your data like this
       echo $br_bank; 
    }
    
        3
  •  0
  •   Sudhi Sr    7 年前

    我终于在 Abdulla Nilam 的答案。我刚刚在数组中传递了值,如下所示

    'br_bank' => $get_bank->bank_name
    

    现在这个观点正在发挥作用。

        4
  •  -1
  •   Haneef Ansari    4 年前

    get_bank_by_branch($branch_list->bankid); 此方法仅返回对象。 $get_bank 此变量由数组访问,但它是一个对象。