Postingan lainnya
Bagaimana cara mengatasi error seperti ini?
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Welcome::$model_barang
Filename: controllers/Welcome.php
Line Number: 23
Backtrace:
File: /home/vol18_2/epizy.com/epiz_28896231/htdocs/application/controllers/Welcome.php
Line: 23
Function: _error_handler
File: /home/vol18_2/epizy.com/epiz_28896231/htdocs/index.php
Line: 315
Function: require_once
An uncaught Exception was encountered
Type: Error
Message: Call to a member function tampil_data() on null
Filename: /home/vol18_2/epizy.com/epiz_28896231/htdocs/application/controllers/Welcome.php
Line Number: 23
Backtrace:
File: /home/vol18_2/epizy.com/epiz_28896231/htdocs/index.php
Line: 315
Function: require_once
INI UNTUK WELCOME.PHP
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$data['barang'] = $this->model_barang->tampil_data()->result();
$this->load->view('templates/header');
$this->load->view('templates/sidebar');
$this->load->view('dashboard',$data);
$this->load->view('templates/footer');
}
}
LALU INI KODING UNTUK MODEL_BARANG.PHP
<?php
class Model_barang extends CI_Model{
public function tampil_data(){
return $this->db->get('tb_barang');
}
public function tambah_barang($data,$table){
$this->db->insert($table,$data);
}
public function edit_barang($where,$table){
return $this->db->get_where($table,$where);
}
public function update_data($where,$data,$table)
{
$this->db->where($where);
$this->db->update($table,$data);
}
public function hapus_data($where,$table){
$this->db->where($where);
$this->db->delete($table);
}
public function find($id)
{
$result =$this->db-> where('id_brg',$id)
-> limit(1)
-> get('tb_barang');
if($result->num_rows()> 0){
return $result->row();
}else{
return array();
}
}
public function detail_brg($id_brg)
{
$result = $this->db->where('id_brg',$id_brg)->get('tb_barang');
if($result->num_rows() >0){
return $result->result();
}else{
return false;
}
}
}
Tanggapan
Sesuaikan judul dengan pertanyaan kamu ya. Baca aturan main cara membuat judul yang baik
3 Jawaban:
<div>Pada welcome controller, tambahkan script untuk me-load file model_barang gini :</div><pre>public function __construct() { parent::__construct(); $this->load->model(['Model_barang' => 'model_barang'); }</pre><div><br>Silahkan cek referensinya disini <a href="https://codeigniter.com/userguide3/general/models.html#loading-a-model">https://codeigniter.com/userguide3/general/models.html#loading-a-model</a></div>
Tanggapan
tetap sama hasilnya gan
<div>Coba method index-nya di ubah seperti ini :</div><pre>public function index() { //Load model $this->load->model('Model_barang'); $data['barang'] = $this->Model_barang->tampil_data()->result(); $this->load->view('templates/header'); $this->load->view('templates/sidebar'); $this->load->view('dashboard',$data); $this->load->view('templates/footer'); }</pre><div><br>Perhatikan penamaan-nya, model_barang dan Model_barang, pastikan menggunakan huruf yg sama, yaitu capitalize di awal nama class modelnya.</div><div><br></div>
Tanggapan
nama class modelnya Model_barang namun tetap sama errornya
<pre>public function index() { $this->load->model('model_barang'); $data['barang'] = $this->model_barang->tampil_data()->result(); $this->load->view('templates/header'); $this->load->view('templates/sidebar'); $this->load->view('dashboard',$data); $this->load->view('templates/footer'); }</pre><div> kalau dibikin seperti ini sudah bisa gan tapi tampilan webnya jadi berantakan <br><figure data-trix-attachment="{"contentType":"image/jpeg","filename":"ss an.JPG","filesize":50830,"height":640,"url":"https://i.ibb.co/kx8Wsk5/ss-an.jpg","width":1011}" data-trix-content-type="image/jpeg" data-trix-attributes="{"presentation":"gallery"}" class="attachment attachment--preview attachment--jpg"><img src="https://i.ibb.co/kx8Wsk5/ss-an.jpg" width="1011" height="640"><figcaption class="attachment__caption"><span class="attachment__name">ss an.JPG</span></figcaption></figure>solusinya bagaimana ya gan?</div>
Tanggapan
Tinggal ganti aja base_url nya