can I use model classe's methods in library methods codeigniter [duplicate] - php

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

Related

CodeIgniter - Unable to Locate Class [duplicate]

This question already has answers here:
Extending The Controller Class in CodeIgniter
(5 answers)
Closed 4 years ago.
I am new to codeIgniter and facing issue while extending a controller. I do know there is answer on stackoverflow but it is relevant to extending a core class and does not provide me solution. As given below, I want to extend 'Auth_controller' but doing so gives me class not found error
MY Auth_controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
abstract class Auth_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->model('login_model');
if(!$this->session->userdata('u_id')){
return redirect(base_url('admin/login'));
}
else
{
if(!$this->login_model->do_check_login($this->session->u_id,$this->session->email,$this->session->session_id,$this->session->role_id))
{
return redirect(base_url('admin/login'));
}
echo "login successful";
}
}
public abstract function index();
}
My Dashboar_controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard_controller extends Auth_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('email');
$this->load->library('calendar');
}
public function index()
{
//my code here
}
}
error
Fatal error: Class 'Auth_Controller' not found in C:\xampp1\htdocs\Admin\application\controllers\admin\Dashboard_controller.php on line 4
You probably need to add require_once('Auth_controller.php') at the top of Dashboard_controller.php; otherwise the Dashboard file will not know about the Auth class. (See require_once in the php docs).
https://stackoverflow.com/a/27910751/594235

Why is my load->view giving me an undefined property in CodeIgniter?

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

Codeigniter call to Member function Error

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();

Including view file in codeigniter

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?

Controller called inside another controller, after that DB calls wont work - Codeigniter

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 :)

Categories