an error occured on file controller (codeigniter) - php

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

Related

Call to undefined method in Codeigniter

I am creating a model there is no error in my case, but it give me error
Message: Call to undefined method Auth_model::chech_user()
Here is my code
Controller
class Auth extends CI_Controller
{
public function __construct()
{
parent::__construct();
$params = array(
'iteration_count_log2' => '8',
'portable_hashes' => TRUE
);
$this->load->library('hash', $params);
$this->load->model('auth_model');
}
public function login()
{
if (isset($_POST) && !empty($_POST)) {
print_r($this->auth_model->chech_user($_POST));
exit;
}
$this->load->view('auth/login');
$this->load->view('layouts/footer');
}
Model
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Auth_model extends CI_Model
{
public function check_user($params = array())
{
echo 'done';
}
}
There's a typo within the code you've provided, you're trying to call chech_user() not check_user() within the controller.
change this line
print_r($this->auth_model->chech_user($_POST));
to
print_r($this->auth_model->check_user($_POST));

Can I load and use a model in MY_Controller using CI3

I'm using Codeigniter 3 and I need some data available to all methods. I will query the data from the database and then I need to display it on every page.
I have created a MY_Controller extending CI_Controller and saved it in /application/core/
But I am getting this error:
Fatal error: Call to undefined method Area_model::get_user_locations()
MY_Controller:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public $location_data;
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('area_model');
$org_id = $this->session->userdata('org_id');
$this->location_data = $this->area_model->get_user_locations($org_id);
}
}
Can i access models and the database from within MY_controller?
Area_model.php
// get all areas for an organisation
public function get_user_locations($org_id) {
$areas = $this->db->get_where('areas', array('org_id' => $org_id));
$area_array = $areas->result_array();
return $area_array;
}
MY_Controller
not used to direct any function to __construct()
__construct to only load to any model
write other function to MY_Controller look like this
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public $location_data;
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('area_model');
$org_id = $this->session->userdata('org_id');
}
}
function functionname()
{
$this->location_data = $this->area_model->get_user_locations($org_id);
}
Area_model.php
public function get_user_locations($org_id)
{
$areas = $this->db->get_where('areas', array('org_id' => $org_id));
$area_array = $areas->result_array();
return $area_array;
}

Message: Unable to locate the model you have specified - codeigniter

my model file name is M_zwall.php :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class M_zwall extends CI_Model {
public function __construct(){
parent::__construct();
}
public function getLastWallpapers($start = 0,$end = 0)
{
$this->db->select('*');
$this->db->from('wallpapers');
$this->db->limit(10,$start);
return $this->db->get()->result();
}
}
?>
I loaded this model in my controller :
$this->load->model('m_zwall');
but I got this error :
Message: Unable to locate the model you have specified: M_zwall
updated
public function __construct(){
parent::__construct();
$this->load->model('m_zwall');
}
Try this..
$this->load->model('M_zwall');
Try to change file name M_zwall.php to m_zwall.php

Codeigniter index parameter

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';
}
}

What is wrong with my Model and Controller?

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);
}

Categories