This is not the only controller in the folder. I already deleted index.php.
but once i add this part of a function for a shopping cart
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends CI_Controller
{
public function index($index = 0)
{
$data['listProduct'] = $this->mproduct->findAll();
$this->load->view('template/header.php', $data);
$this->load->view('index', $data);
}
}
it gives me this error
Message: Undefined property: Product::$mproduct
Filename: controllers/product.php
Line Number: 7
here is my model
<?php
class MProduct extends CI_Model
{
function _construct()
{
parent::_construct();
}
function findAll()
{
return $this->db->get('product')->result();
}
function find($id)
{
$this->db->where('id', $id);
return $this->db->get('product')->row();
}
}
You need to load the model first before calling it.
Also the first letter should be capitalized with rest of the name should be in lowercase.
Class names must have the first letter capitalized with the rest of
the name lowercase. Make sure your class extends the base Model class.
Refer
Add this:
$this->load->model ( 'Mproduct' );
$data['listProduct'] = $this->Mproduct->findAll();
Full code :
Controller : controllers/Product.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends CI_Controller {
public function index()
{
$this->load->model ( 'Mproduct' );
$data['listProduct'] = $this->Mproduct->findAll();
$this->load->view('template/header.php', $data);
$this->load->view('index', $data);
}
}
Model : models/Mproduct.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mproduct extends CI_Model {
function _construct()
{
parent::_construct();
}
function findAll()
{
echo 'model';
}
function find($id)
{
echo 'model';
}
}
Related
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
I was returning query->result() from my model to controller and in the controller I passed the $data to the view file but I am not getting any data in the view file.
Here is the show_model.php file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Show_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
function get_post_data()
{
$query=$this->db->get('posts');
return $query->result();
}
}
Here is the show.php file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Show extends CI_controller {
public function __construct()
{
parent::__construct();
}
function show_post_data(){
$this->load->model("Show_model");
$data['records'] = $this->Show_model->get_post_data();
$this->load->view('list_view',$data);
}
}
Here is the show_list.php view file:
<div>
<?php echo "<pre>"; print_r($records); exit; ?>
</div>
The name of your view is show_list and in the controller you are using list_view
simply replace the code in the controller for displaying the view with
function show_post_data(){
$this->load->model("Show_model");
$data['records'] = $this->Show_model->get_post_data();
$this->load->view('show_list',$data);
}
I'm working on a calendar planning provided by [easyappointments][1] but now I'm stuck on this error by yesterday. In the main page users.php I've added the following:
<?php
require_once("application/models/impostazioni_model.php");
$this->load->model('impostazioni_model');
$this->impostazioni_model->load_colours();
?>
the require_once find correctly the file impostazioni_model.php but when I enter in the page users.php I see this error:
Fatal error: Call to a member function load_colours() on a non-object
on this line: $this->impostazioni_model->load_colours();
in the class impostazioni_model.php I've this content:
<?php if ( ! defined('BASEPATH')) exit('Direct execution not allowed.');
class Impostazioni_Model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function load_colours()
{
echo "print";
}
}
?>
I've followed the codeigniter documentation, in particular the class model must be in capital letter so I don't know what I'm wrong. Someone could help me out?
In Controller
Path - application/controllers
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Admin extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('impostazioni_model'); //this will show "Print" word on browser.
}
function index() {
$this->impostazioni_model->load_colours();
}
}
In Model
Path - application/model
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Impostazioni_model extends CI_Model {
function __construct() {
parent::__construct();
}
function load_colours() {
echo "print";
}
}
I have placed this controller on application/core, called MY_base.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Base_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
}
function set_spaces($post_array, $primary_key){
$post_array['nombre']=str_replace(" ", "_", $post_array['nombre']);
return $post_array;
}
}
?>
and this controller on the controllers folder
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Clase extends Base_Controller {
function __construct(){
parent::__construct();
$this->load->library('grocery_CRUD');
}
function index(){
if($this->session->userdata('logged_in')){
$this->grocery_crud->set_table('clase');
$this->grocery_crud->set_subject('Clase');
$this->grocery_crud->required_fields('nombre');
$this->grocery_crud->unset_columns('id');
$this->grocery_crud->unset_print();
$this->grocery_crud->unset_export();
$this->grocery_crud->unset_add();
$this->grocery_crud->callback_before_update(array($this,'set_spaces'));
//$this->grocery_crud->set_theme('datatables');
$output = $this->grocery_crud->render();
//$this->load->view('header', $data);
$this->load->view('header2.php',$output);
$this->load->view('clase_view', $output);
}
else{
redirect('login', 'refresh');
}
}
}
?>
I think I've followed documentation's instructions correctly but I get this error
Fatal error: Class 'Base_Controller' not found in C:\xampp\htdocs\esi3d\application\controllers\clase.php on line 3
It looks like is looking for base_controler on controllers folder not in core folder.
Place your MY_base.php also in controllers folder
For the life of me I cannot understand why I am receiving errors with my install of CodeIgniter and this current MVC set up.
The error I see is
Fatal error: Call to undefined method Login::users_model() in /var/www/application/controllers/login.php on line 17
Controller application/controllers/login.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$data = '';
$this->load->model('users_model');
$data['testing'] = $this->users_model()->test_user();
$this->load->view('home',$data);
}
}
Model application/models/users_model.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function test_user() {
return 'Test User #1';
}
}
View application/views/home.php
echo $testing;
No need for function bracket with model name
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$data = '';
$this->load->model('users_model');
$data['testing'] = $this->users_model->test_user();
$this->load->view('home',$data);
}
}
Replace $data['testing'] = $this->users_model()->test_user(); with $data['testing'] = $this->users_model->test_user(); in your controller.
Simply load your model inside the constructor
public function __construct()
{
parent::__construct();
$this->load->model('users_model');
}
Then simply call the relevant functions inside any controller functions like this.
public function index()
{
$data = '';
$data['testing'] = $this-> users_model-> test_user();
$this->load->view('home',$data);
}