Hi all I am having an error and I just can't see why codeigniter is throwing the error:
controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Management extends CI_Controller {
public function index(){
//calls login page view
$this->managementView();
}
public function managementView(){
//loads course page view
$users['users'] = $this->management_model->getInfo();
$this->load->view("header");
$this->load->view("users", $users);
$this->load->view("footer");
}
}
Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Management_model extends CI_Model{
function getInfo(){
$query = $this->db->get("users");
$result = $query->result_array();
return $result;
}
}
I am receiving the following error, I just can't seem to see what the hell I'm doing wrong - I'm new to web stuff so don't know what these errors indicate:
Fatal error: Call to a member function getInfo() on a non-object
also see this:
Severity: Notice
Message: Undefined property: Management::$management_model
Filename: controllers/management.php
Line Number: 12
I can't see the issue? - If anyone could point it out would be much appreciated.
you need to load the model before calling any function from the model.
for eg:
$this->load->model('management_model');
$users['users'] = $this->management_model->getInfo();
or you can load it via the constructor.
function __construct() {
parent::__construct();
$this->load->model('management_model');
}
}
the notice you are getting is clearly telling about the undefined property management model
you can see more about this here
You dont appear to ever set the management_model property in your controller.
I would expect to see something like this somewhere in your controller:
$this->management_model = new Management_model();
Initially you can load the model like this
$this->load->model('management_model');
then only u can use the object to call function
you need some more clarity Click
use
$this->load->model('management_model');
before
$users['users'] = $this->management_model->getInfo();
Related
please I need help, I'm looking for the mistake from 1 day ago
Severity: Notice
Message: Undefined property: Login::$Usuario_model
Filename: api/login.php
Line Number: 27
Fatal error: Call to a member function obtener_usuario() on a non-object in /home/fpincheira/web/cobranza/application/controllers/api/login.php on line 27
this is my model usuario_model.php. This query is a example, my real query is other
<?php
class Usuario_model extends CI_Model {
function __construct(){
parent::__construct();
$this->db1 = $this->load->database('db1', TRUE);
}
function obtener_usuario($rut){
$sql = "SELECT sysdate FROM dual"; //this is a example query
$query = $this->db1->query($sql, array($rut));
return $query->result_array();
}
}
?>
The controller login.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH.'/libraries/REST_Controller.php';
class Login extends REST_Controller {
function __construct(){
parent::__construct();
}
function validarUsuario_post(){
try{
$username = $this->post('user');
$this->load->model('Usuario_model','usuario');
$perfil_usuario = $this->usuario->obtener_usuario($username);
//print_r($perfil_usuario);
}catch(Exception $e){
$this->response(array('error'=>$e->getMessage()), $e->getCode());
}
}
}
?>
In my autoload.php I have loaded 'database' library.
Can you see the mistake?
You need replace the following lines :
$this->load->model('Usuario_model','usuario');
$perfil_usuario = $this->usuario->obtener_usuario($username);
with :
$this->load->model('usuario_model');
$perfil_usuario = $this->usuario_model->obtener_usuario($username);
Try and let me know by commenting.
answer the question.
I had to create the object, because to import the model codeigniter not create the instance the object.
$this->load->model('usuario_model');
$usuario = new usuario_model();
$perfil_usuario = $usuario->obtener_usuario($username);
this way I could access to function of model.
If somebody have a best answer, pliss comment, thanks
Regards
usuario_model.php and $this->load->model('Usuario_model','usuario'); Don't you think lowercase and uppercase model name is making difference. While calling to load model from controller U is in caps but actual model filename is in lowercase. So, model file is missing/not found.
are you getting $username value in controller? this link should help read the question part. include function used before CodeIgniter class changes the view loading order
I haven't used CodeIgniter in over a year. I remember it was useful for quick, simple projects but I seem to have fallen at the first hurdle here. I can't seem to load my default view. Here is the controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
private $data = array();
public function __constructor() {
parent::__construct();
}
public function home() {
$this->load->view('header');
$this->load->view('nav');
$this->load->view('home');
$this->load->view('footer');
}
}
This gives me:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Home::$load
Filename: controllers/home.php
Line Number: 13
But I can't figure out why.
In my config I have set 'html', 'url' and 'form' to autoload. And my routes defaults correctly to 'home'. It's kinda frustrating because I know it's something really simple that I'm forgetting here.
Your __constructor is wrong. Use __construct instead of __constructor
public function __construct()
{
parent::__construct();
}
Your method has same name with the view you are calling which is home
I am a newbie in Codeigniter. I am doing a project in which in every pages, I would like to include a drop down for showing multi-languages. For this, I am including a view file in one of the view file in another controller as:
<?php $this->load->view('language/alllang');?>
In alllang.php, I would like to include the code for displaying drop down. For this I have created a controller LanguageController with the following code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Language extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
}
function alllang()
{
$data['val']="hhh"; // for testing
$this->load->view('alllang',$data);
exit;
}
}
Code for alllang.php is:
<?php
echo $val;
?>
But I am getting an error like this:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: val
Filename: language/alllang.php
Line Number: 2
This code is only for testing purpose (not the code for multilanguage). How can I set the value to be included in view file from controller.
Edited for database access functionality:
You could just create a library file and save it in application/libraries, call it "language_lib.php":
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Language_lib{
function __construct(){
$this->CI =& get_instance();
}
function lang_dropdown(){
//db queries:
$q = $this->CI->db->select('*')->from('table')->get()->result_array();
$html = … //your dropdown code here using $q
return $html;
}
}
Next, in application/config/autoload.php:
$autoload['libraries'] = array('language_lib');
Now in each controller method that needs the drop down, e.g. the index method:
function index(){
$data['dropdown'] = $this->language_lib->lang_dropdown();
$this->load->view('some_view', $data);
}
You can access this in the view with <?php echo $dropdown; ?>
Your explanation is a bit confusing, but by doing this
$this->load->view('language/alllang');
You're not calling a controller, you're calling a view only, without passing any data. If I get it right, you have a parent view, then inside it you'd like to call a language drop down view? Well, in this case a drop down view should be called with data array, and data should be coming from controller calling a parent view. Does it make sense?
I have a controller, which calls another controller, and after that a DB call happens, now when i run the DB call it doesn't work, the error i get is
<p>Message: Undefined property: Advertisement::$admodelobj</p>
<p>Filename: v1/Advertisement.php</p>
This is how my controller looks
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Advertisement extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function getads()
{
//another controller call
require_once 'application/controllers/v1/ip2locale.php';
$GetLocale = new ip2locale();
$range = $GetLocale->index($clientip);
//now the db call
$this->load->model('ads_model','admodelobj');
$campaigns = $this->admodelobj->getCampaigns('desktop',1.00,'IN');
}
}
Now if i just put the db call above the "Another controller call" it works good, but right after the "another controller call" it gives me the error, what could be the issue?
Fixed it, one has to load the controller back again to avoid the confusion, and it starts working :)
$ci =& get_instance();
$ci->load->model('ads_model','admodelobj');
$campaigns = $ci->admodelobj->getCampaigns($deviceTypeValue,$payout,$locale);
Great Brij Raj Singh... Just tell, I have one way :
public function getads(){
$this->load->library('../controllers/ip2locale');
$range = $this->ip2locale->index($clientip);
.....
}
And it would works :)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“PHP Notice: Undefined property”
I try to use external library in my CI web. I refer these links
https://www.codeigniter.com/user_guide/general/creating_libraries.html
and CodeIgniter custom library error: Call to a member function on a non-object to make this work
but I get following error message
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Dataloading::$load
Filename: libraries/dataloading.php
Line Number: 28
What I try is load data for combo boxes from library.
here is the code of library class
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dataloading {
public function __construct() {
}
public function index()
{
}
public function loadcombo(){
$this->load->model('dataOperateModel');
//Calling the getcombo_titel() function to get the arr of titles. Model already loaded.
$arrStates = $this->dataOperateModel->getcombo_titel();
//Getting the final array in the form which I will be using for the form helper to create a dropdown.
foreach ($arrStates as $job_name) {
$arrFinal[] = $job_name->title;
}
$data['job_name'] = $arrFinal;
$data['main_content']='home/welcome_message';
//Passing $data to the view, so that we can get the states as an array inside the view.
$this->load->view('layout',$data);
}
}
Here is the code of welcome class
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
//this condition will check whether user has logged in, otherwise
//he will be redirect to login
if (!$this->session->userdata('logged_in'))
{
redirect('admin/admin_login');
}
// $this->load->model('dataOperateModel');
}
public function index()
{
//$this->load->view('welcome_message');
$this->load->library('dataloading');
$this->dataloading->loadcombo();
//$this->loadcombo();
}
}
can anybody explain where I have done the mistake.
You need to load Codeigniter instance in order to use Codeigniter core and libraries
$this->ci =& get_instance();
then you can reference as below
$this->ci->load(.....)
and it's better to check how to create your own library