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
Related
I am trying to use session in my project. It works on localhost but not on web server. I read some articles about that in stackoverflow and ellislab, but it didn't work for my project. Probably problem is about creating ancillary classes.
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Memberlogin extends CI_Controller {
//protected $CI;
function __construct()
{
$CI =& get_instance();
parent::__construct();
$CI->load->library('session');
//$this->load->library('form_validation');
//$this->load->helper(array('form', 'url'));
$CI->load->helper('date');
$CI->load->helper('ip_address');
$CI->load->model('Sql_model');
}
Now errors are :
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Line Number: 14
and
A PHP Error was encountered
Severity: Error
Message: Call to a member function library() on a non-object
Line Number: 14
Try this code,
function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper('date');
$this->load->helper('ip_address');
$this->load->model('Sql_model');
}
If class is in controllers directory, you can directly get the CI instance by extending the CI_Controller and use $this.
Than, try without calling get_instance() function. Just like docs says:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Memberlogin extends CI_Controller {
//protected $CI;
function __construct()
{
parent::__construct();
$this->load->library('session');
//$this->load->library('form_validation');
//$this->load->helper(array('form', 'url'));
$this->load->helper('date');
$this->load->helper('ip_address');
$this->load->model('Sql_model');
}
This will make you crazy. but it works.
In autoload.php
$autoload['libraries'] = array('database', 'session');
$autoload['helper'] = array('url','date','ip_address');
and load model in __constructor
public function __construct()
{
parent::__construct();
$this->load->model('Sql_model');
}
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();
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
I'm trying to get familiar with CI and I have run into a problem while trying to implement my Model. I get the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Home::$OrderModel
Filename: controllers/home.php
Line Number: 12
My assumption was that I was breaking some convention with the naming of the Model. If
I change the line in question and call the model using all lower case:
$data['query'] = $this->ordermodel->get_all_workorder_names();
Nothing is returned to the view.. blank page; no source, no error.
Here is my model:
<?php
class OrderModel extends Model{
function OrderModel()
{
parent::Model();
$db = $this->load->database();
}
function get_all_workorder_names()
{
$this->db->select('name');
$query = $this->db->get('WorkOrder');
return $query->result();
}
}
?>
This is the calling controller:
<?php
class Home extends Controller{
function Home()
{
parent::Controller();
$this->load->model('ordermodel');
$this->load->helper('url');
}
function index()
{
$data['query'] = $this->OrderModel->get_all_workorder_names();
$this->load->view('Header');
//$this->load->view('Home',$data);
$this->load->view('Footer');
}
}
?>
What am I doing wrong? A side question: Is there a debugger available for PHP in Eclipse?
Use $this->ordermodel. The variable name will match whatever you use with load->model()
For further reading, you can check out CodeIgniter's User Guide entry on loading models here.