CodeIgniter Call to a member function on a non-object in [library] - codeigniter

I'am trying call custom library function witch should return inputed values back to form, but I'am geting error.
enter image description here
Controler:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Guest extends CI_Controller {
public function __construct(){
parent::__construct();
error_reporting(E_ALL ^ (E_NOTICE));
$this->load->helper(array('form', 'url', 'date'));
$this->load->library('form_validation', 'uri');
$this->load->model('dayData');
$this->load->library('session');
$this->load->library('guest');
}
public function index(){
$this->login();
}
public function dataToView($data, $viewFile){
$this->load->view('template/header');
if($viewFile != ''){
$this->load->view('user/' . $viewFile, $data);
}
$this->load->view('template/footer');
}
public function login(){
$this->dataToView($data, 'guest/login');
}
public function registration(){
if($this->input->post('registrationSubmit') !== null) {
//$this->form_validation->set_error_delimiters('<div class="alert alert-warning" role="alert">', '</div>');
$this->config->load('form_validation');
$this->form_validation->set_rules($this->config->item('registrationValidation'));
if($this->form_validation->run() == FALSE){
var_dump($this->guest->registrationPost());
$this->dataToView($data, 'guest/registration');
} else {
echo "string";
}
} else {
$this->dataToView($data, 'guest/registration');
}
}
}
Library:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Guest {
protected $CI;
public function __construct(){
// Assign the CodeIgniter super-object
$this->CI =& get_instance();
//$this->CI->load->model('Guest');
$this->CI->lang->load('error', 'english');
}
public function registrationPost(){
$result = array('name' => $this->CI->input->post('name'),
'nickName' => $this->CI->input->post('nickName'),
'email' => $this->CI->input->post('email'));
return $result;
}
}

There is a naming conflict between the controller and the custom class. They should not have the same name.

You have duplicate class names. The controller class name is Guest and the library class name is also Guest.
When CodeIgniter goes to load the Guest library, it will skip over it and log a debug message. https://github.com/bcit-ci/CodeIgniter/blob/develop/system/core/Loader.php#L1032
I suggest renaming your library to something else, Registration perhaps?

Related

Redirect is not working in Codeigniter on real server. Error reporting in on but not showing any error

I have Codeigniter 2 installed on my server. redirect is not working on the login page. I can provide the URL.
This is my controller file of login. I have set the error reporting on but not displaying any error. Kindly help me with this. The script is taken from truebus.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login_admin extends CI_Controller {
public function __construct() {
parent::__construct();
date_default_timezone_set("Asia/Kolkata");
$this->load->model('AdminLogin_model');
$this->load->model('Settings_model');
$this->load->library('session');
if($this->session->userdata('logged_in_admin')) {
redirect(site_url('bus_details/view_busdetails'));
}
}
}
public function index(){
$template['page_title'] = "Login";
if(isset($_POST)) {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');
if($this->form_validation->run() == TRUE) {
// redirect(base_url().'Bus_details/view_busdetails');
redirect(site_url('bus_details/view_busdetails'));
}
}
$this->load->view('login-form');
}
I think you didn't include URL helper
$this->load->helper('url');
you should include in controller
$this->load->helper('url);
redirect()

creating a model and controller to get category list from db using codeigniter

1.models/calists.php // My model file
Model file here i get the category list from database to the controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Catlists extends CI_Model
{
public function __construct()
{
$this->load->database(); //load database
}
public function getCategories()
{
$query = $this->db->get_where('category',array('status'=>'Enable'));
if ($query->num_rows() > 0)
{
return $query->result();
}
else
{
return array();
}
}
}
?>
2.controllers/catlist.php // controller file
Controller to get the data from model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Catlist extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('catlists');
}
public function catlist()
{
$data['catlist'] = $this->catlists->getCategories();
$this->load->view('home', $data);
}
}
In header am printng the categories list
print_r($data['catlist']);
I am not sure what error you are getting here, but when you are loading model in controller, first letter should be capital.
$this->load->model('Catlists');
$data['catlist'] = $this->Catlists->getCategories();
In header you have to call
print_r($catlist);
The file name must match the class name.
For example
if your class is this:
class User_model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
Your file will be this:
application/models/User_model.php
Here in you case your model name is
models/Calists.php
So your model file be
Model Calists.php
class Calists extends CI_Model {
function __construct()
{
parent::__construct();
}
public function getCategories()
{
$query = $this->db->get_where('category',array('status'=>'Enable'));
if ($query->num_rows() > 0)
{
return $query->result();
}
else
{
return array();
}
}
}
And you call this model file in you controller like this
Controller
public function __construct()
{
parent::__construct();
$this->load->model('calists');
}
CodeIgniter anatomy of model.

codeigniter help causing error while loading model

I am not sure whats wrong with my code.......it causing an error while loading model.......
please help...........
my controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Exams extends CI_Controller {
public function index(){
$ob = new exam_class();
$ob->set_exam(0,'Html exam','1','20');
$ob->create_exam();
echo 'success';
}
}
class exam_class{
private $id;
private $title;
private $catagory;
private $timeLength;
function set_exam($id,$title,$catagory,$timeLength){
$this->id = $id;
$this->title = $title;
$this->catagory = $catagory;
$this->timeLength = $timeLength;
}
function create_exam(){
$this->load->model('examModel');
$this->examModel->create_exams($title,$catagory,$timeLength);
}
}
model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ExamModel extends CI_Model {
public function create_exams($title,$catagory,$timeLength){
$data = array(
'title' => $title ,
'catagory' => $catagory ,
'timeLength' => $timeLength
);
$this->db->insert('exams', $data);
}
}
error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: exam_class::$load
Filename: controllers/exams.php
Line Number: 26
Fatal error: Call to a member function model() on a non-object in C:\xampp\htdocs\exam\application\controllers\exams.php on line 26
You shouldn't put more than one class in a file. Controller should be something like this.
class Exams extends CI_Controller {
private $id;
private $title;
private $catagory;
private $timeLength;
public function __construct()
{
parent::__construct();
$this->load->model('examModel');
}
public function index(){
$this->set_exam(0,'Html exam','1','20');
$this->create_exam();
echo 'success';
}
function set_exam($id,$title,$catagory,$timeLength){
$this->id = $id;
$this->title = $title;
$this->catagory = $catagory;
$this->timeLength = $timeLength;
}
function create_exam(){
$this->examModel->create_exams($title,$catagory,$timeLength);
}
}
Along with #shin i want to include that go to this file
....\testproject\application\config\autoload.php
and edit this to add your models
$autoload['model'] = array('modelName1','modelName2');
and to load the models from any time from any controller . This will automatically load your models.No need to add
$this->load->model('modelName');
Tip : Keep it simple
In your controller :
public function __construct()
{
parent::__construct();
$this->load->model('examModel');
}
public function index()
{
$exam_data = $this->process_exam_data(0,'Html exam','1','20');
$insert_status = $this->examModel->create_exams($exam_data);
if($insert_status===TRUE){
echo "Exam Insert Successful!";
}
else{
echo "Exam Insert Failed";
}
}
public function process_exam_data($id, $title, $category, $timelength)
{
// Do whatever you want with the data, calculations etc.
// Prepare your data array same as to be inserted into db
$final_data = array(
'title' => $processed_title,
'catagory' => $processed_category,
'timeLength' => $processed_time
);
return $final_data;
}
And in your model :
public function create_exams($data)
{
$result = $this->db->insert('exams', $data); // Query builder functions return true on success and false on failure
return $result;
}
Your index function is the main function which does the calling, whereas all the processing work is done in the process_exam_data function.
Have a nice day :)

How to pass variables in codeigniter 2

I have following
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Hello extends CI_Controller {
var $name = 'test';
function index() {
$this->name = 'Andy';
$data['name'] = $this->name;
$this->load->view('you_view', $data); // THIS WORKS
}
function you() {
$data['name'] = $this->name;
$this->load->view('you_view', $data); // BUT THIS DOESN'T WORK
}
}
My question is how to I pass the $this->name = 'Andy'; to you() ??
Since it is being set in a different method of the controller, which equates to another request in your code, you will need to store it in a session variable to have it persist across page requests.
function index() {
$this->name = 'Andy';
$data['name'] = $this->name;
$this->session->set_userdata('name', $this->name);
$this->load->view('you_view', $data); // THIS WORKS
}
function you() {
$data['name'] = $this->session->userdata('name');
$this->load->view('you_view', $data); // BUT THIS DOESN'T WORK
}
If its a value that is part of the class you can put it in the constructor
class Hello extends CI_Controller {
public function __construct() {
parent::__construct();
// will be available to any method in the class
$this->name = 'andy';
}

how can i make my own $this->load->view in codeigniter

the default of CI of loading views is:
$this->load->view('path');
but what if i wanted to do something like
$this->load->adminView('path')
then i can prefix the path in adminView followed by the path
how would i do this?
thanks
go to ../System/Core/Loader.php, line 417 -> 210 (CI 2.10)
public function view($view, $vars = array(), $return = FALSE)
{
return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
}
change your function name (and maybe some code else) as you wish, be careful!
In application/core/ make a new Controller:
<?php
if(!defined('BASEPATH'))
exit('No direct script access allowed');
class Admin_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function load_admin_view($path, $data = '', $return = false)
{
return $this->load->view("admin_dir/" . $path, $data, $return);
}
}
?>
Then make your current controller extend this controller:
class Page extends Admin_Controller
Instead of
class Page extends CI_Controller
Then you can use:
$this->load_admin_view("path");

Resources