Call to undefined method, Database error - php

Can anybody tell me what's wrong with my code? It said that the problem is in the controller, on line 13. Please help
Controller
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('model_databis'); //line13
}
function index(){
$data=array('data' => $this->model_databis->bus());
$this->load->view('admin/databis',$data);
}
Model
class Model_databis extends CI_Model{
function __construct(){
parent::__construct();
}
function dbis(){
$query = $this->db->get('bus');
return $query->result_array();
}
function tambah($data){
$this->db->insert('bus', $data);
return TRUE;
}
}

You can try this solution for your problem :
Please change your function name in controller.
function index(){
$data=array('data' => $this->model_databis->dbis()); // bus into dbis
$this->load->view('admin/databis',$data);
}

Related

Call to undefined method in Codeigniter 3

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.

Getting a drop down list from a database in CodeIgniter

I am new to CodeIgniter and I have been trying to populate the drop down list on the view page with data from the database with no success. I tried using the recomendations from this question but the drop down is still empty (display data from database to dropdown CodeIgniter)
Here is my view:
<label>City</label>
<select class="form-control>
<option value="">All</option>
<?php
foreach($groups as $city)
{
echo '<option value="'.$city['cityidd'].'">'.$city['city'].'</option>';
}
?>
</select> <br/>
Here is my controller:
<?php
class Main_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
public function index()
{
$this->load->helper('form');
$this->load->view('supplier_add');
}
}
Here is my model:
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT city FROM citys');
return $query->result();
}
}
The table name is "citys" then the corresponding column heads are "cityidd" and "city"
There are several issue found there. Make changes as below
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load_model('Site_model');
$this->load->database();
}
public function index(){
$this->load->helper('form');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
Finally model
function getAllGroups(){
$query = $this->db->query('SELECT cityidd,city FROM citys');
return $query->result_array();
}
and now test
First change your SELECT query as
SELECT cityidd, city FROM citys
In the index() of controller change the code as
public function index()
{
$this->load->helper('form');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
you have to call your model method and pass it to view in the controller, code:
public function index()
{
$this->load->helper('form');
$this->load->model('site_model');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
if you are working on linux dont forget upper and lowercase names!
class Main_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
$this->load->model('Site_model');
}
public function index()
{
$this->load->helper('form');
$data['groups']=$this->Site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
}
Here is my model:
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT * FROM citys');
return $query->result();
}
}
Try like this
Make these changes
IN Model convert data to array as you are using it as array in view so change to this
function getAllGroups()
{
$query = $this->db->query('SELECT * FROM citys');
return $query->result_array();
}
In Controller
public function index()
{
$this->load->helper('form');
$this->load->model('site_model');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}
You are not loading model load the Model
for example:-
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Site_model')
$this->load->database();
}
public function index()
{
$this->load->helper('form');
$data['record']=$this->Site_model->getAllGroups()
$this->load->view('supplier_add', $data);
}
}
Model:-
class Site_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT city FROM citys');
return $query->result();
}
}

Message: Undefined property: News::$news_model in Codeigniter

I am learning Codeigniter from Codeigniter mannual step by step. Therefore i used as it is code from the manual.
The Controller class is :
<?php
class News extends CI_Controller{
public function _construct()
{
parent::_construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header',$data);
$this->load->view('news/index',$data);
$this->load->view('templates/footer');
}
?>
The Model Class is:
class News_model extends CI_Model{
public function _construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news',array('slug'=>$slug));
return $query->row_array();
}
?>
Please help me to fix this error. I've Tried every possible solution on internet but couldn't find any mistake.
CodeIgniter uses __construct with two _.
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
The above code should work.
As a side note, you can call your models like this:
$this->load->model('news_model', 'news');
and then you can call it like this:
$this->news->get_news();
But your method works fine, just makes it a bit easier.

404 Page Not Found (The page you requested was not found.)

I try to show (*localhost/ujian/index.php/user_controller*)
but not found, why?
Code
class User_controller extends CI_Controller{
function __construct()
{
parent ::__construct();
$this->load->model('user_model');
$this->load->helper(array('form'));
}
function user()
{
$data['judul'] = 'Daftar User';
$this->load->view('user', $data);
}
function simpan_user()
{
$this->user_model->simpan_user();
$data['notifikasi'] = 'Data berhasil disimpan';
$data['judul']='Insert Data Berhasil';
$this->load->view('notifikasi', $data);
redirect('user_controller');
}
function view()
{
$data['daftar_user'] = $this->user_model->get_user_all();
$this->load->view('daftar_user', $data);
}
function delete_user($id)
{
$data['daftar_user'] = $this->user_model->get_user_all();
$this->load->view('daftar_user', $data);
$username = $this->user_model->delete_user($id);
redirect('user_controller');
}
I try to show (*localhost/ujian/index.php/user_controller*)
but not found, why?
As per your question, you are calling the URL:
localhost/ujian/index.php/user_controller
But your controler doesn't have default function index(). So add default function and do default action for this url.
function index()
{
// add your action
// or call any default function like $this->user();
}
When You don't Specify any function name after controller name, It call the index() function by default.
In your Requested URL there is no function name specified, So, It by default call the index() function . But There is no Index() function defined in your requested controller .
Please , Create an index() function and try.
Cheers..!!

Calling Curl(ed) json content from Code Igniter

This is my function in model
<?php
class Clothes_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_clothes()
{
$url="http://192.168.0.105/ci/json_source/clothes.php";//clothes.php contain json data
$data= this->curl->simple_get($url);
$result = json_decode($data,true);
return $result;
}
}
This is my controller
<?php
class Clothes extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('clothes_model');
}
public function view($result)
{
$data['clothes_item'] = $this->clothes_model->get_clothes();
if (empty($data['clothes_item']))
{
show_404();
}
$data['title'] = $data['clothes_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('clothes/view', $data);
$this->load->view('templates/footer');
}
}
When I tried to run I got this error
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in
/var/www/ci/application/models/clothes_model.php on line 12
Is there anything wrong?
try changing:
this->curl->simple_get($url);
to
$this->curl->simple_get($url);
And make sure your $this->curl is valid
I did like this and works for me
$string=file_get_contents("proj-data/food_query.php"); //place where json file is
$decoded=json_decode($string,TRUE);

Categories