Codeigniter undefined property on core/Model.php - 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:
edit.
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.

Related

Undefined property: update_user::$update_model - codeigniter

I am a noob at both PHP and codeigniter, so please be gentle.
I was messing around with this file and got this error.
Message: Undefined property: update_user::$update_model
Filename: controllers/update_user.php
Fatal error: Call to a member function update_user_id() on null in C:.....update_user.php on line 20
here is my code
<?php
class update_user extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('user_model');
}
function show_user_id() {
$id = $this->uri->segment(3);
$data['users'] = $this->update_model->show_users();
$data['single_user'] = $this->update_model->show_user_id($id);
$this->load->view('update_view', $data);
}
function update_user_id() {
$id= $this->input->post('did');
$data = array(
'userName' => $this->input->post('dname'),
'userPass' => $this->input->post('dpass'),
);
$this->update_model->update_user_id($id,$data);
$this->show_user_id();
}
}
this is line 20
$this->update_model->update_user_id($id,$data);
anybody can give any pointers to what is wrong?
thanks in advance
This line:
$this->update_model->update_user_id($id,$data);
Should be:
$this->user_model->update_user_id($id,$data);
Also, every other place where you have used update_model, it should be changed to user_model.
This is assuming that you have only a user_model and no update_model. That's what your code shows.
The error is indicating the problem clearly. you are using update_model model which you haven't loaded in your update_user Controller. If this is it than load the update_model like this $this->load->model('update_model'); else change the code:
$this->update_model->update_user_id($id,$data);
to
$this->user_model->update_user_id($id,$data);
Also please share your model code if there is any issue.

Code igniter undefined variable results issue

I'm trying to load photos from the database via a for each loop to display each photo in a formatted div. however i keep receiving a undefined variable results and i am unsure why
this is my view code
<?php echo form_open(base_url('index.php/smite/getimages/1/1'));?>
<?php foreach($imgres as $imgdata){?>
<img class="img-box" src="<?php echo base_url("assets/img/".$imgdata['name'].".".$imgdata['type']);?>">
<p style="font-size:140%"><?php echo $imgdata['description']?></p>
<?php }?>
<?php echo form_close();?>
this is the controller code
class Smite extends CI_Controller {
private $data = "";
private $session_data = array();
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library(array('form_validation', 'session'));
$this->load->model(array('users_model','comments_model','images_model'));
}
public function getimages($g1,$g2){
$this->data['imgres'] = $this->images_model->get_images($g1,$g2);
$this->load_page("smite");
}
furthermore in the controller is a function to load the page view and send it data
private function load_page($page)
{
$this->data['page'] = $page;
$this->load->view('template/head');
$this->load->view('template/nav',$this->data);
$this->load->view($page."_view",$this->data);
$this->load->view('template/footer');
$this->load->view('template/login_register_modal');
$this->load->view('template/scripts',$this->data);
}
the query runs fine and returns results when tested on the mysql server but im not sure why its not returning results to the view. these are the sever error messages
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: imgres
Filename: views/smite_view.php
Line Number: 28
followed by
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/smite_view.php
Line Number: 28
change your controller like this
public function getimages($g1,$g2){
$data['imgres'] = $this->images_model->get_images($g1,$g2);
$this->load->view("smite",$data);
}
Then In get_images fuction in images_model write the query to get images data from database

How to pass an array to the main view in Codeigniter

It seems that I just can't pass an array in another function into main view.
My index() function is =>
public function index()
{
$this->load->view('anasayfa');
}
My aforementioned function is =>
public function pozisyon_tutma()
{...
....
print_r(array_values($pozisyon));
$GLOBALS['pozisyong'] = $pozisyon;
}
which works...
I am seeking something like =>
public function index()
{
$this->pozisyon_tutma();
$data['hoppa']=$GLOBALS['pozisyong'];
$this->load->view('anasayfa', $data);
}
and I am seeking in view 'anasayfa' (which is the main view) something like this code to work=>
<?php
print_r(array_values($hoppa));
?>
However this code gives the following error=>
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: hoppa
Filename: views/anasayfa.php
Line Number: 19
Backtrace:
File: D:\wamp\www\proje\application\views\anasayfa.php
Line: 19
Function: _error_handler
File: D:\wamp\www\proje\application\controllers\Welcome.php
Line: 24
Function: view
File: D:\wamp\www\proje\index.php
Line: 292
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: array_values() expects parameter 1 to be array, null given
Filename: views/anasayfa.php
Line Number: 19
Backtrace:
File: D:\wamp\www\proje\application\views\anasayfa.php
Line: 19
Function: array_values
File: D:\wamp\www\proje\application\controllers\Welcome.php
Line: 24
Function: view
File: D:\wamp\www\proje\index.php
Line: 292
Function: require_once
All help will be much appreciated. I checked all other threads in stackoverflaw and could not make any of them work in my case... Thank you all
You should use in your controller:
$data = array('key1'=>$value1, 'key2'=>$value2, 'key3'=>functionX()....);
$this->load->view('yourview.php',$data);
And in the view to access to the array of values:
echo $key1; // prints $value1.
echo $key2; // prints $value2.
Also, you could use $_SESSION to store some variables in CI. Hope it helps.
I did it in the following way=>
public function index()
{
require('Hesaplama.php');
$Hesaplama = new Hesaplama();
$data['hoppa']=$Hesaplama->pozisyon_tutma();
$this->load->view('anasayfa', $data);
}
It works. I returned $pozisyon which is an array as #mic suggested. I will keep the suggestions of #JP. Aulet in case I need them in the near future. Thank you all...

How to call a model from view

Hi i am trying to call a model from view but i am getting error as.
A PHP Error was encountered Severity: Notice Message: Undefined property:
CI_Loader::$Stopsmap_Model Filename: stopsmap/index.php Line Number: 59
i tryed this below code in view.But no luck
foreach ($routes as $route_details):
//calling model
//$trip_max_min_sequence_details = $this->load->Stopsmap_Model->fullTripdetails($trips->trip_id); i tried this also but same error
//$this->load->model('Stopsmap_Model');
//$data = $this->stopsmap_model->fullTripdetails($route_details->route_id); i tried this also but same error
$this->load->model('fixedtransit_model/Stopsmap_Model');
$get_route_related_trips = $this->Stopsmap_Model->getRoutetrips($route_details->route_id);
if(count($get_route_related_trips)>=1):
echo '1 stop';
endif;
endforeach;
thanks in advance for the help
In your controller , you can have like this:
function routes(){
//call model
$this->load->model('fixedtransit_model/Stopsmap_Model');
$routes = $this->topsmap_model->get_routes();
foreach ($routes as $route_details){
$get_route_related_trips[] = $this->Stopsmap_Model->getRoutetrips($route_details->route_id);
}
$data['related_routes'] = $get_route_related_trips;
//load view and pass data
$this->load->view('myview' , $data);
}
and in your view:
<?php foreach($related_routes){
//do something
}
?>
on my end
i have created a model named accountmodel in applications/models dir
and load in view like (would be best if you load model in your controller):-
$this->load->model('AccountModel','Account');
//AccountModel (model class name)
//Account (object) you can further use this model using object
for more info :- http://ellislab.com/codeigniter/user-guide/general/models.html

Calling model from codeigniter library shows error

Hello I have created a library Auth.php in application/libraries to authenticate if the current user is logged in or not. So my controller is :
$session_data = $this->session->userdata('logged_in');
$this->load->library('Auth');
$auth = new Auth;
$user_id = $auth->authenticate($session_data);
And the library :
class Auth{
function authenticate($vars){
$CI =&get_instance();
$CI->load->model('adminmodels/login_model');
$username = $vars['username'];
$password = $vars['password'];
$user_id = $vars['user_id'];;
$user_type = $vars['user_type'];
$check_login = $this->login_model->login($username,$password); //Line 14
if($check_login){
$user_id = $user_id;
}else{
$user_id = 0;
}
return $user_id;
}
}
But it is showing error like :
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Auth::$login_model
Filename: libraries/Auth.php
Line Number: 14
Whats wrong I am doing ??
While correct generally, in CI you should call the library like this, not with the new keyword:
$this->load->library('Auth');
$user_id = $this->auth->authenticate($session_data);
Also, since you assigned the global CI object to a variable, you can't use $this to refer to it:
$check_login = $this->login_model->login($username,$password); //Line 14
should be:
$check_login = $CI->login_model->login($username,$password); //Line 14
(since you loaded the model there: $CI->load->model('adminmodels/login_model'); )
Inside Codeigniter library file, you cannot load any model, config, or any external library using $this by pass without CI instance object/variable
For your case in Auth.php at line 14, you are wrong :
$check_login = $this->login_model->login($username,$password); //Line 14
It should be like this (if access from internal function only) :
$check_login = $CI->login_model->login($username,$password); //Line 14
Or, if access from any functions in the Auth class :
$check_login = $this->CI->login_model->login($username,$password); //Line 14
But first, you must make CI intance object in your construct function like this :
$this->CI =&get_instance();
$this->CI->load->model('adminmodels/login_model');
before you action in second alternative above.
May help you...
Thanks

Categories