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);
}
?>
Related
I have an error with the following file, and specifically, I assume, the following line:
$data['events'] = $this->homes->get_events();
The error is:
Message: Undefined property: CI_Loader::$homes
I am in the controller file, where I assume the problem is.
File name: /application/controllers/Home.php
$data['events'] = $this->homes->get_events(); <?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function _construct()
{
parent::_construct();
$this->load->model('homes');
}
public function index()
{
$data['events'] = $this->homes->get_events();
$this->load->view('template/header.php');
$this->load->view('home/index.php', $data);
$this->load->view('template/footer.php');
}
}
There is a model file at models/Homes
We tried changing the name of the model file to 'homes' (lower case) and also the code to 'Homes', but the same error persisted.
Specifically the error on running the url is:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$homes
Filename: controllers/Home.php
Line Number: 15
Backtrace:
File: public_html/anchor/application/controllers/Home.php
Line: 15
Function: _error_handler
File: /public_html/anchor/index.php
Line: 315
Function: require_once
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.
<?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();
Please help me in this...I am learning CodeIgniter and i got stuck on this error.There must be some silly mistake.
I am using xampp software for this.
It keeps popping undefined variable rows.
table name is 'user'
view file name is try
model file name is trydb
controller file name is condb
ERROR
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: rows
Filename: views/try.php
Line Number: 12
Backtrace:
File: C:\xampp\htdocs\ciagain\application\views\try.php
Line: 12
Function: _error_handler
File: C:\xampp\htdocs\ciagain\application\controllers\Welcome.php
Line: 27
Function: view
File: C:\xampp\htdocs\ciagain\index.php
Line: 292
Function: require_once
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/try.php
Line Number: 12
Backtrace:
File: C:\xampp\htdocs\ciagain\application\views\try.php
Line: 12
Function: _error_handler
File: C:\xampp\htdocs\ciagain\application\controllers\Welcome.php
Line: 27
Function: view
File: C:\xampp\htdocs\ciagain\index.php
Line: 292
Function: require_once
View File
<title>
Connecting data base
</title>
</head>
<body>
<?php
foreach($rows as $r){
echo $r->ID;
echo $r->Name;
}
?>
</body>
</html>
Modal File
<?php
class Site_model extends Model {
function getAll() {
$q = $this->db->get('user');
if($q->num_rows()>0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
?>
Controller file
<?php
class Site extends CI_Controller {
function index() {
$this->load->model('trydb');
$data['rows'] = $this->trydb->getAll();
$this->load->view('try', $data);
}
}
?>
Default controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
public function tryagain(){
$this->load->view('try');
}
}
Why has you put foreach loop in model? you can use this:
function getAll() {
$q = $this->db->get('user');
$data = $q->result();
return $data;
}
and as suggested by DS9 , use below in view files.
if(isset($rows) && sizeof($rows)>0)
{
foreach($rows as $r)
{
}
}
It is not error, it is a notice
Severity: Notice
To resolve this you can defined the variable or disable the notice. Prefer first one.
like,
if(sizeof($rows)>0)
{
foreach($rows as $r){
}
}
Delete space between } and ?> on line 12. It will help. Also there is no Codeigniter 4 :)
Is this posible to send data from controller like this?
Controller test.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test extends CI_Controller {
public $data = array(
'modul' => 'user',
'title_meta' => 'User',
'breadcrumb' => 'User',
'pesan' => '',
'pagination' => '',
'main_view' => 'administrator/user',
'form_action' => '',
'form_value' => '',
);
public function __construct()
{
parent::__construct();
$this->load->model('test_model', 'test', TRUE);
}
public function index()
{
$data['query'] = $this->test->get_all();
$this->load->view('test',$data);
}
}
Model test_model.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Test_model extends CI_Model {
function get_all() {
$query = $this->db->get('snb_user');
return $query->result_array();
}
}
View test.php
<?php
foreach($query as $row)
{
echo $row['UserID'];
echo $row['UserName'];
echo $row['UserEmail'];
}
echo $breadcrumb;
?>
When i run this, i get this error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/test.php
Line Number: 4 A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/test.php
Line Number: 4 A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/test.php
Line Number: 4 A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/test.php
Line Number: 4 A PHP Error was encountered
Severity: Notice
Message: Undefined variable: breadcrumb
Filename: views/test.php
Line Number: 7
I want to parse data from variable $data on controller and the result of sql query from model, is it posible?
Any help would be awesome, thanks!
You haven't declare varible named $breadcrumb in view file or in controller.if you want to remove this notice then you should decalare varible or use isset() or empty() method before using $breadcrumb varible.