Postingan lainnya
Kenapa saya tidak bisa menjalankan fungsi yang ada di Model ?? CodeIgniter
hay gan, langsung aja ya sesuai pertanyaan
ini controller saya
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index()
{
$data['title'] = 'Login' ;
$this->load->view('templates/header',$data);
$this->load->view('pages/login');
$this->load->view('templates/footer');
}
public function check_login(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('Login_model');
$this->load->model('Login_model');
$this->login_model->get_login($username,$password);
}
}
ini model saya
class Login_model extends CI_Model {
public function __construct(){
parent::__construct();
$this->load->database();
echo 'login model tampil/';
}
public function get_login($username,$password){
echo $username;
}
}
*modelnya emang belum jadi , cuma buat ngetes bisa jalan atau gak.
ini error saya ![](https://skspace.sgp1.digitaloceanspaces.com/forums/images/error model.jpg)
kalo di liat dari errornya . Model berhasil di jalanin tapi fungsi check_login yang di dalem Model yang ga bsa berjalan. mohon pencerahanya . thanks
8 Jawaban:
$this->login_model->get_login($username,$password);
// [ $this->login_model ] ini tidak ada,
//kecuali bikin property baru, di bagian atas controller sebelumnya
//dan dioper nilainya ke propery tadi
public $login_model;
$this->login_model = $this->load->model('Login_model');
muncul ini kang Hilman
Fatal error: Call to undefined method CI_Loader::get_login() in C:\xampp\htdocs\ci\application\controllers\Login.php on line 28
controllernya
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public $login_model;
public function __construct(){
parent::__construct();
}
public function index()
{
$data['title'] = 'Login' ;
$this->load->view('templates/header',$data);
$this->load->view('pages/login');
$this->load->view('templates/footer');
}
public function check_login(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->login_model = $this->load->model('login_model');
$this->login_model->get_login($username,$password);
}
}
?>
maaf, ngga merhatiin detail, di codeigniter emang bisa cara yang pertama, tanpa perlu dikasih ke property lain. 1. kode yang pertama, di metode check_loginnya, kenapa manggilnya dua kali modelnya? 2. Coba load m odelnya di constructor
public function __construct(){
parent::__construct();
$this->load->model('login_model');
}
public test(){
.....
$this->login_model->get_login($username,$password);
}
baik gan hilman, nanti di rumah saya coba :D
belum bisa jalanin fungsi d Metode nya gan hilman
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public $login_model;
public function __construct(){
parent::__construct();
$this->login_model = $this->load->model('login_model');
}
public function index()
{
$data['title'] = 'Login' ;
$this->load->view('templates/header',$data);
$this->load->view('pages/login');
$this->load->view('templates/footer');
}
public function check_login(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->login_model->get_login($username,$password);
}
}
?>
atau yang ini
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public $login_model;
public function __construct(){
parent::__construct();
$this->login_model = $this->load->model('login_model');
}
public function index()
{
$data['title'] = 'Login' ;
$this->load->view('templates/header',$data);
$this->load->view('pages/login');
$this->load->view('templates/footer');
}
public function check_login(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->get_login($username,$password);
}
}
hasilnya
Fatal error: Call to undefined method CI_Loader::get_login() in C:\xampp\htdocs\ci\application\controllers\Login.php on line 28
line 28 itu
$this->login_model->get_login($username,$password);
// atau di coba pake ini juga sama
$this->get_login($username,$password);
Jawaban Terpilih
saya udah ngetest controller:
public function index()
{
$this->load->view('welcome_message');
$this->load->model('blog');
$this->blog->testing();
}
model:
<?php
class Blog extends CI_Model {
public function __construct()
{
// Call the CI_Model constructor
parent::__construct();
}
public function testing()
{
die('this is testing');
}
}
dan berhasil, jadi secara struktur ini udah bener cara manggilnya. Ada kesalahan lain. Cek lagi 1. penamaan file, 2. lokasi model apakah langsung di bawah folder model , 3. coba test metode lain sekedar die, bisa atua ngga
Iya gan ane juga bingung, tutorial pertama yang buat negluarin news juga bisa mangggil Class di Model dengan lancar. and scrip saya di atas juga function __controllernya yang di modeul udah bisa runing , tapi kenapa malah function lainya ga bsa ke panggil ya :D nanti saya coba lagi :)...
Bisa gan akirnya. tapi belum tau yang mana yang bikin bisa . ane sempet ubah nama fungsi sama hapus cookies di browser
controllernya :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library(array('session'));
$this->load->helper(array('url'));
}
public function index()
{
$data['title'] = 'Login' ;
$this->load->view('templates/header',$data);
$this->load->view('pages/login');
$this->load->view('templates/footer');
}
public function check_login(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('login_model');
$this->login_model->login_user($username,$password);
}
}
?>
modelnya
<?php
class Login_model extends CI_Model {
public function __construct(){
parent::__construct();
$this->load->database();
echo 'login model tampil/';
}
public function login_user($username,$password){
echo $username;
echo $password ;
}
}
?>
and model berhasil echo masukan dari form di file view :D
solved setelah seminggu lamanya hihihihi.................