An uncaught Exception was encountered in codeigniter - php

An uncaught Exception was encountered
Type: Error
Message: Call to undefined function file_exits()
Filename: C:\wamp64\www\blog\application\controllers\Pages.php
Line Number: 7
Backtrace:
File: C:\wamp64\www\blog\index.php
Line: 315
Function: require_once
<?php
class Pages extends CI_Controller
{
public function view($page = 'home')
{
if(!file_exits(APPPATH.'views/pages/'.$page.'.php'))
{show_404();}
$data['title'] = ucfirst($page);
$this->load->view('templates/header');
$this->load->view('pages/'.$page,$data);
$this->load->view('templates/footer');
}
}
?>

It's a typo. file_exits() ? It should be file_exists(). Voting to close.

Related

Message: Undefined property: Todo::$uri

I use codeigniter.
I get an error when I try to get the parameter in the url
<?php
class Todo extends CI_Controller{
public function __construct() {
parent::__construct();
}
public function index(){
$this->load->view('index.php');
}
public function blog(){
$this->load->view('arsiv');
}
public function test($param){
$this->load->view('test');
$a = $this->uri->segment(2);
}
}
when i try to go : http://localhost/cod/index.php/todo/test/123
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Todo::$uri
Filename: controllers/Todo.php
Line Number: 21
Backtrace:
File: /opt/lampp/htdocs/cod/application/controllers/Todo.php
Line: 21
Function: _error_handler
File: /opt/lampp/htdocs/cod/index.php
Line: 315
Function: require_once
An uncaught Exception was encountered
Type: Error
Message: Call to a member function segment() on null
Filename: /opt/lampp/htdocs/cod/application/controllers/Todo.php
Line Number: 21
Backtrace:
File: /opt/lampp/htdocs/cod/index.php
Line: 315
Function: require_once
Please verify the URL helper is enabled in your config function
try to use $this->uri->get_segment(1);
Try to use
$CI =& get_instance();
$this->CI->uri->segment('2')
you have to load helper in codigniter autoload.php file in config folder
$autoload['helper'] = array('url');
Try to use
codigniter autoload.php file in config folder
$autoload['helper'] = array('url');
Url : http://localhost/laravel/users/abc
$product_id = $this->uri->segment(2);
Result : abc
Read https://www.codeigniter.com/user_guide/libraries/uri.html

Search form in CodeIgniter

I am having a problem while creating a search form in CodeIgniter - it's showing errors like:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Login::$membership_model
Filename: controllers/login.php
Line Number: 12
Backtrace:
File: C:\xampp\htdocs\Project5_1\CodeIgniter\application\controllers\login.php
Line: 12
Function: _error_handler
File: C:\xampp\htdocs\Project5_1\CodeIgniter\index.php
Line: 315
Function: require_once
Fatal error: Call to a member function getSearchBook() on null in C:\xampp\htdocs\Project5_1\CodeIgniter\application\controllers\login.php on line 12
A PHP Error was encountered
Severity: Error
Message: Call to a member function getSearchBook() on null
Filename: controllers/login.php
Line Number: 12
Backtrace:
My Controller Page is:
<?php
class Login extends CI_Controller{
function search(){
$searchBook = $this->input->post('searchBook');
$data = $this->membership_model->getSearchBook($seachBook);
$this->load->view('main_content',$data);
}
?>
My Model page is:
class Membership_model extends CI_Model{
function getSearchBook($searchBook) {
$this->db->select('*');
$this->db->from('book');
$this->db->like('author',$searchBook);
return $query->result();
}
}
My main_content.php is:
<?php
echo form_open('login/search');
echo form_input('searchBook','Search Book', 'id="searchBook"');
echo form_submit('submit', 'Search');
echo form_close();
?>
<table><th>Title</th></table>
<div>
<?php
// List up all results.
foreach ($results as $val)
{
echo $val['title'];
}
?>
</div>
</body>
It's been a long time since I used CodeIgniter, but from what I remember, you're not loading the model in your Login class constructor. That's why you're getting these two parts of the error:
Message: Undefined property: Login::$membership_model
Message: Call to a member function getSearchBook() on null
Neither the property or the function exist in the Login class if you haven't loaded the membership_model model.
Off the top of my head, I think it needs to be something like this in your Login controller:
<?php
class Login extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->model('membership_model');
}
function search(){
$searchBook = $this->input->post('searchBook');
$data = $this->membership_model->getSearchBook($seachBook);
$this->load->view('main_content',$data);
}
?>

CodeIgniter News Tutorial - Parse Error

I'm following the CodeIgniter tutorial (http://www.codeigniter.com/user_guide/tutorial/news_section.html), when I get to 'Display the news' it isn't made clear where to add the second code block:
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
the only way I can interpret the tutorial, is to add it to the News.php controller.
<?php
class News extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
}
public function view($slug = NULL)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
}
when I add it at the end of application/controllers/News.php I get a parse error:
Parse error: parse error in /server/application/controllers/News.php on line 33
A PHP Error was encountered
Severity: Parsing Error
Message: parse error
Filename: controllers/News.php
Line Number: 33
Backtrace:
if I include it within the News class, I get two PHP errors & a fatal error:
Fatal error: Cannot redeclare News::index() in /server/application/controllers/News.php on line 32
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /server/application/controllers/News.php:32)
Filename: core/Common.php
Line Number: 573
Backtrace:
A PHP Error was encountered
Severity: Compile Error
Message: Cannot redeclare News::index()
Filename: controllers/News.php
Line Number: 32
Backtrace:
Not sure what else to try?
Thanks in advance.
The second code block function get_news()should go in the News_model.php file. This is how your final controller & model will look like:
Model: http://screencast.com/t/XQtJpEnSC
Controller: http://screencast.com/t/Kmec1vGh

HMVC model cannot be loaded SAYS Message: Undefined property:

hi i am currently working on a second module of my hmvc file and there comes an error that i think it wont load the models of this module. the previous modules models, works fine. what may be the error of this one? here is my codes.
error
Severity: Notice
Message: Undefined property: CI::$lEmploymentStatus_Model
Filename: MX/Controller.php
Line Number: 59
Backtrace:
File: C:\xampp\htdocs\TLC_HR\application\third_party\MX\Controller.php
Line: 59
Function: _error_handler
File: C:\xampp\htdocs\TLC_HR\application\modules\EmploymentStatus\Controllers\EmploymentStatus.php
Line: 43
Function: __get
File: C:\xampp\htdocs\TLC_HR\index.php
Line: 292
Function: require_once
controller - EmploymentStatus.php
<?php
class EmploymentStatus extends MY_Controller{
public function __construct(){
parent::__construct();
}
// VIEW REDIRECTING /////////////////////////////////////////////////////////
public function index(){
$data['content_view'] = 'EmploymentStatus/empstat_read';
$this->templates->admin_template($data);
}
public function add_view(){
$data['content_view'] = 'EmploymentStatus/add_view';
$this->templates->admin_template($data);
}
// CREATE /////////////////////////////////////////////////////////
public function create(){
$this->load->library('form_validation');
$this->load->model('EmploymentStatus_Model');
$this->form_validation->set_rules('ES_NAME','Name','trim|required|min_length[2]|max_length[20]');
$this->form_validation->set_rules('ES_DESCRIPTION','Description','trim|required|max_length[50]');
if($this->form_validation->run() == FALSE){
$this->add_view();
}else{
if($query = $this->lEmploymentStatus_Model->insert()){
$this->add_view();
}else{
$this->add_view();
}
}
}
}
?>
model - EmploymentStatus_Model.php
<?php
class EmploymentStatus_Model extends CI_Model{
///// CREATE /////////////////////////////////////////////////////////
public function insert(){
$input = array(
'ES_NAME' => $this->input->post('ES_NAME'),
'ES_DESCRIPTION' => $this->input->post('ES_DESCRIPTION')
);
$insert = $this->db->insert('employment_status',$input);
return $insert;
}
}
?>
it works perfectly fine. It just have a typo.
if($query = $this->lEmploymentStatus_Model->insert()){
$this->add_view();
lEmploymentStatus must be EmploymentStatus

Controller - Model in CodeIgniter and "Undefined property"

<?php
class Success_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
//insert into user table
function get_all()
{
$query = $this->db->get('session'); // = select * from session
return $query->result();
}
}
<?php
class Success extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper('url');
$this->load->model('success_model');
}
public function index()
{
$data= $this->Success_model->get_all();
$this->load->view('success_view', $data);
}
}
?>
As You can see this is good, but I have error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Success::$Success_model
Filename: controllers/success.php
Line Number: 15
Backtrace:
File: C:\xampp\htdocs\session\application\controllers\success.php
Line: 15
Function: _error_handler
File: C:\xampp\htdocs\session\index.php
Line: 292
Function: require_once
Fatal error: Call to a member function get_all() on null in C:\xampp\htdocs\session\application\controllers\success.php on line 15
A PHP Error was encountered
Severity: Error
Message: Call to a member function get_all() on null
Filename: controllers/success.php
Line Number: 15
Backtrace:
I'm looking, and looking, but I dont know what is bad. The good directories are files, other classes are working but this not.
Please help. I'm just tired, because after 2 hours searching mistakes...
For CI v3, afaik
I think I see it:
$data= $this->Success_model->get_all();
Should be:
$data = $this->success_model->get_all(); // Notice the lowercase success_model
PHP variables are case-sensitive where as functions and class names are not.
One more solution would be to change
$this->load->model('success_model');
into
$this->load->model('Success_model');
because then you can use
$data= $this->Success_model->get_all();

Categories