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 :)
Related
I'm using the CI framework and I'm trying to make a foreach loop so that all products of my site are displayed.
This is my controller file:
class AlleCadeausController extends CI_Controller {
public function index()
{
$this->load->model('allecadeaus_model');
$data['cadeaus'] = $this->allecadeaus_model->get_cadeaus();
$this->load->view('allecadeaus');
}
This is my model file:
<?php
class Allecadeaus_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_cadeaus()
{
$query = $this->db->query('SELECT * FROM products');
$result = $query->result_array();
return $result;
}
}
this is my view:
<?php
foreach ($cadeaus as $cadeau)
{
echo $cadeau->product_naam;
}
?>
The error is:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: cadeaus
Filename: views/allecadeaus.php
Line Number: 3
Backtrace:
File: /home/ubuntu/workspace/application/views/allecadeaus.php Line: 3
Function: _error_handler
File:
/home/ubuntu/workspace/application/controllers/AlleCadeausController.php
Line: 11 Function: view
File: /home/ubuntu/workspace/index.php Line: 315 Function:
require_once
My table name is products
You havn't passed the $data variable in your controller. Please follow below code:
$this->load->view('allecadeaus', $data);
So, your controller function should be like below.
public function index()
{
$this->load->model('allecadeaus_model');
$data['cadeaus'] = $this->allecadeaus_model->get_cadeaus();
$this->load->view('allecadeaus', $data);
}
Cheers, you are done.
public function index()
{
$this->load->model('allecadeaus_model');
$data['cadeaus'] = $this->allecadeaus_model->get_cadeaus();
$this->load->view('allecadeaus',$data);
}
try this in you controller
You haven't pass data to the view you just load view.
for passing data to view you have to do
$this->load->view('allecadeaus',$data);
Here are good documentation for passing data from controller to view Contact ME
You should pass the data to the view follow the below code:
public function index()
{
$this->load->model('allecadeaus_model');
$data['cadeaus'] = $this->allecadeaus_model->get_cadeaus();
$this->load->view('allecadeaus, $data');
}
You have to pass $data with the view.
$this->load->view('allecadeaus',$data);
You need pass array of data in second argument.
Please use this :
$this->load->view('allecadeaus',$data);
If you load child view in allecadeaus, then you don't need to pass array of data. You can directly access all variables from array of data.
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);
}
?>
why i can't access it?
my model
News_model.php :
<?php
class News_model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function get_news(){
$query = $this->db->get['news'];
return $query->result_array();
}
}
?>
my Controller
news.php :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
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();
$data['title'] = 'arsip';
$this->load->view('news/index', $data);
}
}
?>
my view
news/index.php
<?php
foreach ($news as $news_item) { ?>
<h1><?php echo $news_item['title']; ?> </h1>
// <p><?php echo $news_item['text']; ?> </p>
<? } ?>
?>
routes :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['news'] = 'news';
$route['default_controller'] = 'halaman/view';
$route['(:any)'] = 'halaman/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
and then acces
http://localhost/codeigniter/index.php/news
the output is :
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_DB_mysqli_driver::$get
Filename: models/News_model.php
Line Number: 11
Backtrace:
File: /opt/lampp/htdocs/codeigniter/application/models/News_model.php
Line: 11 Function: _error_handler
File: /opt/lampp/htdocs/codeigniter/application/controllers/News.php
Line: 14 Function: get_news
File: /opt/lampp/htdocs/codeigniter/index.php Line: 315 Function:
require_once
Fatal error: Call to a member function result_array() on null in
/opt/lampp/htdocs/codeigniter/application/models/News_model.php on
line 12 A PHP Error was encountered
Severity: Error
Message: Call to a member function result_array() on null
Filename: models/News_model.php
change Your $this->db->get();
It's should not be array notation
$this->db->get['news'];
it should be like
$this->db->get('news');
alternatively u can use
$this->db->select('*');
$this->db->from('news');
$query = $this->db->get();
return $query->result_array()
You have to give the table name like this:-
public function get_news(){
$query = $this->db->get('news');
return $query->result_array();
}
Refer this link http://www.codeigniter.com/user_guide/database/examples.html#query-builder-query
<?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();
Thanks a lot for answering my question before
So I have another problem in my Codeigniter, it's still the same project, just counting data rows in my database.
What I would like to do is creating a function in a Controller, and then call that function in a View.
So here's my ORIGINAL code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dash_control extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('dash_model');
}
public function index()
{
$data['cashtotal']=$this->dash_model->totalCash();
$data['approvedtocash']=$this->dash_model->cashapproved();
$data['rejectedtocash']=$this->dash_model->cashrejected();
$this->load->view('dashboard',$data);
}
}
and then here's my view code:
<thead>
<tr>
<th>Bank Name</th>
<th>Total Transaction</th>
<th>Cash Approved</th>
<th>Cash Rejected</th>
</tr>
</thead>
<tbody>
<tr class="warning">
<td>Awesome Bank</td>
<td><?php echo $cashtotal; ?></td>
<td><?php echo $approvedtocash; ?></td>
<td><?php echo $rejectedtocash; ?></td>
</tr>
</tbody>
With the codes above, everything works fine. Until I modified my code like this one below (with the same view file like above):
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dash_control extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('dash_model');
}
public function index()
{
$this->load->view('dashboard');
}
public function anotherfunction()
{
$data['cashtotal']=$this->dash_model->totalCash();
$data['approvedtocash']=$this->dash_model->cashapproved();
$data['rejectedtocash']=$this->dash_model->cashrejected();
$this->load->view('dashboard',$data);
}
}
After that I got this error on my browser:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: cashtotal
Filename: views/dashboard.php
Line Number: 147
Backtrace:
File: C:\xampp\htdocs\application\views\dashboard.php
Line: 147
Function: _error_handler
File: C:\xampp\htdocs\application\controllers\dash_control.php
Line: 18
Function: view
File: C:\xampp\htdocs\index.php
Line: 292
Function: require_once
What did I do wrong? Is there something I'm missing?
So I can't create multiple function inside my controller?
Thank you before.
UPDATE
I followed Saty's guide and it worked. Thanks a lot!
Since this is a table, I would like to add another function, so I add it up like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dash_control extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('dash_model');
}
public function index()
{
$this->CashTransSum();
$this->PayTransSum();
}
public function CashTransSum()
{
$data['cashtotal']=$this->dash_model->totalCash();
$data['approvedcash']=$this->dash_model->cashapproved();
$data['rejectedcash']=$this->dash_model->cashrejected();
$this->load->view('dashboard',$data);
}
public function PayTransSum()
{
$data['paytotal']=$this->dash_model->totalPay();
$data['approvedpay']=$this->dash_model->payapproved();
$data['acqrejectedpay']=$this->dash_model->payrejected();
$this->load->view('dashboard',$data);
}
}
And then I got the same error:
Severity: Notice
Message: Undefined variable: paytotal
Filename: views/dashboard.php
Line Number: 156
Backtrace:
File: C:\xampp\htdocs\application\views\dashboard.php
Line: 156
Function: _error_handler
File: C:\xampp\htdocs\application\controllers\dash_control.php
Line: 29
Function: view
File: C:\xampp\htdocs\application\controllers\dash_control.php
Line: 17
Function: CashTransSum
File: C:\xampp\htdocs\index.php
Line: 292
Function: require_once
It says here that I need to call it ONCE... Which one? Did I put the code wrong?
You need to call anotherfunction() inside your index function as
public function index()
{
$this->anotherfunction();// call your function
}
public function anotherfunction()
{
$data['cashtotal']=$this->dash_model->totalCash();
$data['approvedtocash']=$this->dash_model->cashapproved();
$data['rejectedtocash']=$this->dash_model->cashrejected();
$this->load->view('dashboard',$data);
}
the error you've got, it's completely normal, you need to fill the $data array with all variables, try this out :
protected $data;
public function index()
{
$this->CashTransSum();
$this->PayTransSum();
$this->load->view('dashboard', $this->data);
}
public function CashTransSum()
{
$this->data['cashtotal']=$this->dash_model->totalCash();
$this->data['approvedcash']=$this->dash_model->cashapproved();
$this->data['rejectedcash']=$this->dash_model->cashrejected();
}
public function PayTransSum()
{
$this->data['paytotal']=$this->dash_model->totalPay();
$this->data['approvedpay']=$this->dash_model->payapproved();
$this->data['acqrejectedpay']=$this->dash_model->payrejected();
}