简体   繁体   中英

Codeigniter undefined property on core/Model.php

Controller

    public function editItem(){
         $this->load->helper('form'); 
         $this->load->model('ItemModel'); 
         $data['items'] = $this->ItemModel->itemlist(); 
         $item_details = $this->ItemModel->edititem($this->input->get('id'));    
         $data2['item_name'] = $item_details->name; //THIS IS LINE 28
         $data2['item_description']= $item_details->description; 
         $data2['item_price'] = $item_details->price;
         $this->load->view('item/item_edit',$data2); 
    }

There's an error to my view, and I don't know why

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Item::$name

Filename: core/Model.php

Line Number: 77

Backtrace:

File: C:\xampp\htdocs\itwa213\application\controllers\Item.php
Line: 28
Function: __get

File: C:\xampp\htdocs\itwa213\index.php
Line: 292
Function: require_once

I already checked my autoload on config and it's properly configured with

$autoload['libraries'] = array('database');

You could try this

public function editItem() {
     $this->load->helper('form'); 
     $this->load->model('ItemModel'); 

     $data['items'] = $this->ItemModel->itemlist(); 

     $item_details = $this->ItemModel->edititem($this->input->get('id'));    

     $data2['item_name'] = $item_details['name']; //THIS IS LINE 28
     $data2['item_description'] = $item_details['description']; 
     $data2['item_price'] = $item_details['price'];

     $this->load->view('item/item_edit',$data2); 
}

Your controller function change to like this

public function editItem(){ 
  $this->load->helper('form');
  $this->load->model('ItemModel');

  $data['items'] = $this->ItemModel->itemlist(); 
  $data2['item_details'] =$this->ItemModel->edititem($this->input->get('id')); 

  $this->load->view('item/item_edit',$data2); 
} 

and in your view page

echo $item_details->name;

You should look at your code for the get function you use. try to replace the $this->input->get('id') to existing id number first. check whether it success or not. If success by using existed id number, so it should your get function not working or something wrong. i'm seldom use $this->input->get() but $this->input->post().

on your edit link, you can use: <a href="controller/method/id">edit</a> . so the id is the third segment uri. use $this->uri->segment(3); to get the third segment and store into $item_id variable into your controller. so, no need to use $this->input->get(); to get the item_id value.

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