So i just want to make simple form to add, update, and delete some data(Name, id, and address). Add and delete works fine for me, but the update doesn't, codeigniter give me a error message :
A PHP Error was encountered
Severity: Error
Message: Call to undefined method Mahasiswa_model::getMahasiswa()
Filename: controllers/Mahasiswa.php
Line Number: 35
Backtrace:
And these is my code which i thought its related with the error.
This one file name is Mahasiswa.php
<?php
if (!defined('BASEPATH')) exit('no direcet script access allowed');
class Mahasiswa extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('Mahasiswa_model');
}
public function index() {
$data['mhs'] = $this->Mahasiswa_model->retrieve();
$this->load->view('Mahasiswa_view', $data);
}
public function form_tambah() {
$this->load->view('Tambah_view');
}
public function submit() {
$this->Mahasiswa_model->add($this->input->post('var'));
$data['submitted'] = TRUE;
$this->load->view('Tambah_view', $data);
}
function delete() {
$this->Mahasiswa_model->delete($this->uri->rsegment(3));
$this->index();
}
function form_update() {
$data['mhs'] = $this->Mahasiswa_model->getMahasiswa($this->uri->rsegment(3));
$this->load->view('update_view', $data);
}
function update() {
$this->Mahasiswa_model->update($this->input->post('old_nim'),
$this->input->post('var'));
$this->index();
}
}
?>
And this one is Mahasiswa_model.php
<?php
class Mahasiswa_model extends CI_Model {
function retrieve() {
$query = $this->db->get('mhs');
if($query->result()) {
foreach ($query->result() as $content) {
$data[] = array(
$content->nim,
$content->nama,
$content->alamat
);
}
return $data;
} else {
return FALSE;
}
}
function add($arg) {
$data = array (
'nim' => $arg[0],
'nama'=> $arg[1],
'alamat' => $arg[2],
);
$this->db->insert('mhs', $data);
}
function delete($id) {
$this->db->where('nim', $id);
$this->db->delete('mhs');
}
function update($id, $form) {
$data = array(
'nim' => $form[0],
'nama' =>$form[1],
'alamat' => $form[2],
);
}
}
?>
Maybe someone can help me find where mistake i made? Line 35 not clear enough for me
This line in your controller is the problem:
$this->Mahasiswa_model->getMahasiswa($this->uri->rsegment(3));
You're calling the getMahasiswa method that should be available in your model, but (assuming the code you posted for your model is complete) it's not. Your model only has the retrieve, add, delete and update methods declared.
you either need to create the getMahasiswa method in your model, or you need to call a different method from your controller.
if you want to call a function from same controller then simply write.
$data = $this->getMahasiswa($parameter1);
IMP : function must be present there in the same controller.
Related
Help me.. I am getting error Type: ArgumentCountError Message: Too few arguments to function M_students::get_edit() I wanted to display all entries in my mysql table but i kept on getting error message. I am a newbie in Codeigniter and couldn't really figure out how to solve this.
My Model
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class M_students extends CI_Model {
public function __construct()
{
parent::__construct();
}
private static $table = 'students';
private static $pk = 's_id';
public function get_edit($data, $s_id)
{
return $this->db->set($data)->where(self::$pk, $s_id)->update(self::$table);
}
}
My Controllers
public function edit_data()
{
$this->load->helper(['form', 'notification']);
$s_id = $this->uri->segment(3);
$where = "s_id = '$s_id'";
$data['student'] = $this->m_students->get_edit($where);
if ($this->validation()) {
$u_id = $this->input->post('s_id', TRUE);
$data = [
's_npsn' => $this->input->post('s_npsn', TRUE),
's_namasekolah' => $this->input->post('s_namasekolah', TRUE),
'kota' => $this->input->post('kota', TRUE),
'provinsi' => $this->input->post('provinsi', TRUE),
'u_updated_at' => date('Y-m-d H:i:s'),
'u_updated_by' => $this->session->userdata['s_id'],
'u_is_active' => $this->input->post('u_is_active', TRUE)
];
$this->m_students->edit($data, $u_id);
$this->session->set_flashdata('alert', success('Data user berhasil diperbarui.'));
$data['title'] = "Data ".self::$title;
$data['content'] = "dashboard/student-form";
redirect('student');
} else {
$data['title'] = "Edit ".self::$title;
$data['action'] = site_url(uri_string());
$data['content'] = 'dashboard/student-form';
if (!$s_id) {
redirect('student');
} else {
$this->load->view('dashboard/index', $data);
}
}
}
}
You are not supplying the required arguments to model function get_edit() but only one which is $where.
$data['student'] = $this->m_students->get_edit($where);
Whereas after array named $data calling a model function named edit() with 2 arguments which not existing according to code provided in the question.
$this->m_students->edit($data, $u_id);
You should only call get_edit($data, $u_id) with 2 arguments after $data.
I'm beginner in codeigniter.I'm trying to delete a row from my table.I tried the following code.But it's not working.
This is url i'm passing.
Delete User
This is my controller (Delete_user.php).
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Delete_user extends CI_Controller {
public function delete_row($id) {
$this->load->model('Delete_selecteduser');
$where = array('id' => $id);
$this->Delete_selecteduser->delete_user('users', $where);
}
And here is my model (Delete_selecteduser.php).
<?php
class Delete_selecteduser extends CI_Model {
public function __construct() {
$this->load->database();
}
public function delete_user($table, $where = array()) {
$this->db->where($where);
$res = $this->db->delete($table);
if ($res)
return TRUE;
else
return FALSE;
}
}
And also I want to display a message that record has been deleted successfully.Please help me to fix this.
i suggest that the function must be
public function delete_user($table, $where = array()) {
$this->db->where($where);
$this->db->delete($table);
$res = $this->db->affected_rows();
return $res;
}
in this way you always have a answer, 0 for a no deleted row or 1 if you delete only a row
I get the error :
Fatal error: Call to undefined method select::form() in public_html/opunletter/application/controllers/Welcome.php on line 44.
when i run this. what might be the reason.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
//load the database
$this->load->database();
//load the model
$this->load->model('select');
//load the method of model
$data['h']=$this->select->select();
$data['a']=$this->select->archieve();
$data['n']=$this->select->latest();
$id=$this->input->get("id");
$data['f']=$this->select->load($id);
//return the data in view
$this->load->view('welcome_message', $data);
}
public function login()
{
$this->load->view('userlogin');
}
public function submit()
{
$data = array(
'from' => $this->input->post('from'),
'to' => $this->input->post('to'),
'title' => $this->input->post('title'),
'openletter' => $this->input->post('openletter')
);
$this->load->model('select');
//Transfering data to Model
$this->select->form($data);
}
} ?>
My model
<?php
class Select extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
//we will use the select function
public function Select()
{
//data is retrive from this query
$query = $this->db->get('country');
return $query;
}
public function archieve()
{
//data is retrive from this query
$query = $this->db->query("SELECT * FROM country WHERE archieve=1;");
return $query;
}
public function latest()
{
//data is retrive from this query
$query = $this->db->query("SELECT * FROM country WHERE latest=1;");
return $query;
}
public function load($id)
{
//data is retrive from this query
$query = $this->db->select('*')
->from('country')
->where('open_id', $id)
->get();
return $query;
}
public function form($data){
// Inserting in Table(students) of Database(college)
$query = $this->db->insert('user_openletter', $data);
return $query;
}
}
?>
Hello All
I checked all the posts regarding this ERROR. But my context is little different. Please have a look.
My Model:-
class Post extends AppModel {
function add($data){
if (!empty($data)) {
$this->create();
if($this->save($data)) {
return true ;
}
}
}
//For Add Action
function fetchdata() {
return $this->find('all',array( 'order' => array('id DESC') ) );
}
//For Edit Action
function fetch() {
return $this->Posts->find('all',array( 'order' => array('id DESC')) );
}
function getdata($id) {
return $this->Posts->find('all',array( 'conditions' => array('id'=>$id)) );
}
//For Index Action
function retrievedata($id) {
return $this->Posts->find('all',array( 'conditions' => array('id'=>$id)) );
}
}
Here I am using the EDIT FUNCTION
//For Edit Action
function fetch() {
return $this->Posts->find('all',array( 'order' => array('id DESC')) );
}
My Controller:-
class PostsController extends AppController {
public $uses = array('Post');
public function index(){
if(isset($this->request->params['pass'][0])){
$this->set('showdata',$this->Post->retrievedata($this->request->params['pass'][0]));
}
}
public function add()
{
if(!empty($this->request->data)){
if($this->Post->add($this->request->data)==true){
$this->redirect(array('action'=>'index'));
}
}
}
public function edit(){
$this->set('post',$this->Post->fetch());
if(isset($this->request->params['pass'][0])){
$this->set('showdata',$this->Post->getdata($this->request->params['pass'][0]));
}
}
}
The Edit Function is what I am dealing with
public function edit(){
$this->set('post',$this->Post->fetch());
if(isset($this->request->params['pass'][0])){
$this->set('showdata',$this->Post->getdata($this->request->params['pass'][0]));
}
}
Now the ERROR
Fatal Error
Error: Call to a member function find() on a non-object
File: C:\wamp\www\blogproject\app\Model\Post.php
Line: 51
Your model name is Post not Posts so change Posts->find() to Post->find().
Also for example in Post->fetch() your are in Post model scope so use $this->find() not $this->Post->find().
Read about cake conventions. Model names are singular.
i just try to save input data and in line of save() method show me error , this is my controller register method :
public function register() {
if ($this->Helloworlds->save($this->request->data)) {
$id = $this->Helloworld->id;
$this->request->data['Helloworld'] = array_merge($this->request->data['Helloworld'], array('id' => $id));
$this->Auth->login($this->request->data['Helloworld']);
return $this->redirect('/helloworld/index');
}
else
{
$this->Session->setFlash(__('Sorry no data has saved '));
}
}
and the error :
Error: Call to a member function isNew() on a non-object File
C:\wamp\www\vendor\cakephp\cakephp\src\ORM\Table.php Line: 1092
i would mention this i'm sure about my model name'helloworlds'
any idea?
at least i find out answer , the true code is :
public function register() {
if ($this->request->is('post')) {
$Helloworld = TableRegistry::get('helloworlds'); $Helloworlds = $Helloworld->newEntity($this->request->data); if ($Helloworld->save($Helloworlds)) {
$id = $this->Helloworld->id;
$this->request->data['Helloworld'] = array_merge($this->request->data['Helloworld'], array('id' => $id));
$this->Auth->login($this->request->data['Helloworld']);
return $this->redirect('/helloworld/index');
} else { $this->Session->setFlash(__('Sorry no data has saved ')); } } }