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();
}
Related
I'm about to launch a website which I built with codeigniter3. I've made configuration changes such as base_url, database, etc. But it shows me an error like this :
Message: Undefined property: Home::$home
Filename: controllers/Home.php
Line Number: 18
Backtrace:
File: /home/u8460348/public_html/application/controllers/Home.php
Line: 18 Function: _error_handler
File: /home/u8460348/public_html/index.php Line: 294 Function:
require_once ```
Message: Call to a member function select() on null
Filename: /home/u8460348/public_html/application/controllers/Home.php
Line Number: 18
Backtrace:
File: /home/u8460348/public_html/index.php Line: 294 Function:
require_once ```
So, here is my controller :
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Home extends My_Controller
{
public function index($page = null)
{
$data['title'] = 'Home | Omid Health Style';
$data['content'] = $this->home->select(
[
'product.id', 'product.slug', 'product.title AS product_title', 'product.description',
'product.image', 'product.price', 'product.is_available', 'product.weight',
'category.title AS category_title', 'category.slug AS category_slug'
]
)
->join('category')
->where('product.is_available', 1)
->paginate($page)
->get();
$data['total_rows'] = $this->home->where('product.is_available', 1)->count();
$this->home->table = 'blog';
$data['blogs'] = $this->home->select(
[
'blog.id', 'blog.slug', 'blog.title AS blog_title', 'blog.description', 'blog.content',
'blog.image', 'blog_category.title AS blog_category_title', 'blog_category.slug AS blog_category_slug'
]
)
->join('blog_category')
->paginate($page)
->get();
$data['total_rows'] = $this->home->count();
$data['page'] = 'pages/home/index';
$this->view($data);
}
}
Here is my Home_model.php :
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Home_model extends MY_Model
{
public $table = 'product';
protected $perPage = 8;
}
/* End of file Home_model.php */
And here is in my core folder, because i made a custom configuration like this :
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class My_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$model = strtolower(get_class($this));
if (file_exists(APPPATH . 'models/' . $model . '_model.php')) {
$this->load->model($model . '_model', $model, true);
}
}
// Load view with Default Layouts
public function view($data)
{
$this->load->view('layouts/app', $data);
}
}
If I'm running the program in my localhost, it's totally fine and works. Why do I get this error on a production server?
From the error message you received, the important part is
Message: Call to a member function select() on null
which means your model was not loaded!
as your code works on your localhost environment, but not on the production server, most likely the error is caused by applying wrong Naming Conventions:
File names must be capitalized. For example: Myclass.php Class
declarations must be capitalized. For example: class Myclass Class
names and file names must match.
public function __construct(){
parent::__construct()
}
add this to your Home controller
An uncaught Exception was encountered
Type: Error
Message: Call to undefined method Dashboard::cek_login()
Filename: C:\xampp\htdocs\crud_ajax_ci\application\controllers\Dashboard.php
Line Number: 9
Backtrace:
File: C:\xampp\htdocs\crud_ajax_ci\index.php
Line: 315
Function: require_once
Model file :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Auth_model extends CI_Model{
public function cek_login($username)
{
$hasil = $this->db->where('username', $username)->limit(1)->get('users');
if($hasil->num_rows() > 0){
return $hasil->row();
} else {
return array();
}
}
public function register($table, $data)
{
return $this->db->insert($table, $data);
}
}
Controller file :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dashboard extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->cek_login();
}
public function index()
{
$this->load->view('dashboard');
}
}
For accessing model method you have to load model class First then access Model method.
<?php
class Dashboard extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Auth_model'); // Load Model
$this->Auth_model->cek_login($username); // Load Model Method
}
public function index()
{
$this->load->view('dashboard');
}
}
?>
Note:-
Once loaded Model in Controller, you will access your model methods using an object with the same name as your Model class Name:-
$this->load->model('model_name');
$this->model_name->method();
Check here :-
For Loading Model
https://codeigniter.com/userguide3/general/models.html#loading-a-model
AM trying to connect modal in controller but it showing error like
Fatal error: Call to undefined function customerslist() in C:\xampp\htdocs\CI\application\controllers\Customer.php on line 25
A PHP Error was encountered
Severity: Error
Message: Call to undefined function customerslist()
Filename: controllers/Customer.php
Line Number: 25
Backtrace:
My code is following:
controller : Customer.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Customer extends CI_Controller {
public function __construct(){
parent:: __construct();
$this->load->library('form_validation');
$this->load->model('admin');
$this->load->model('customers');
}
public function index()
{
if($this->session->userdata('Admin')==false)
{
redirect('login');
}
else
{
$data['title'] = "Customer";
$data['customerlist'] = $this->customers>customerslist();
$this->load->view('customer',$data);
}
}
}
Modal: Customers.php
<?php
class Customers extends CI_Model {
function __construct()
{
parent::__construct();
}
//view all Customers
function customerslist()
{
$res = $this->db->get('customers_list');
if($res->num_rows() >0){
foreach($res->result_array() as $row){
$customerlist[] = $row;
}
return $customerlist;
}
else
{
return false;
}
}
//view all Customers
}
How can i fix this issues?
Thanks in advance....
You're missing a - in $this->customers>customerslist();
This should be $this->customers->customerslist();
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
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 :)