0

I have problem call function inside another function in Codeigniter. So i have 2 Function :

Function 1 have Controller For fetch data from database:

public function tampil_halaman_berita()
    {
        $data['tampil_berita'] = $this->berita->getAllBerita();
        $data['tampil_kategori'] = $this->berita->getAllKategori();
        $data['tampil_wartawan'] = $this->berita->getAllWartawan();
    }

Function 2 call Controller from Function 1 to index:

public function index()
    {
        $data['judul'] = "Halaman Berita";
        $this->tampil_halaman_berita();
        $this->load->view('ui/Header');
        $this->load->view('pages/Berita', $data);
        $this->load->view('ui/Footer');
    }

But i get this error, My function tampil_halaman_berita not called up to index()

Severity: Notice

Message: Undefined variable: tampil_berita

Filename: pages/Berita.php

Line Number: 17

Backtrace:

File: C:\xampp\htdocs\flutter-news\application\views\pages\Berita.php
Line: 17
Function: _error_handler

File: C:\xampp\htdocs\flutter-news\application\controllers\pages\Berita.php
Line: 21
Function: view

File: C:\xampp\htdocs\flutter-news\index.php
Line: 315
Function: require_once

I do not want , repeat every Model to all my function like this , So i create some function and i just call function name.

public function add(){
$data['tampil_berita'] = $this->berita->getAllBerita();
        $data['tampil_kategori'] = $this->berita->getAllKategori();
        $data['tampil_wartawan'] = $this->berita->getAllWartawan();
}
public function update(){
$data['tampil_berita'] = $this->berita->getAllBerita();
        $data['tampil_kategori'] = $this->berita->getAllKategori();
        $data['tampil_wartawan'] = $this->berita->getAllWartawan();
}

I hope you all understand my question.

Thank's

IF you need my full Controller

<?php
class Berita extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->model('pages/Berita_model', 'berita');
    }
    public function tampil_halaman_berita()
    {
        $data['tampil_berita'] = $this->berita->getAllBerita();
        $data['tampil_kategori'] = $this->berita->getAllKategori();
        $data['tampil_wartawan'] = $this->berita->getAllWartawan();
    }
    public function index()
    {
        $data['judul'] = "Halaman Berita";
        // $data['tampil_kategori'] = $this->berita->getAllKategori();
        // $data['tampil_wartawan'] = $this->berita->getAllWartawan();
        // $data['tampil_berita_join'] = $this->berita->getBeritaJoin();
        $this->tampil_halaman_berita();
        $this->load->view('ui/Header');
        $this->load->view('pages/Berita', $data);
        $this->load->view('ui/Footer');
    }
    public function add()
    {
        $data['judul'] = " Halaman Berita";
        $data['tampil_berita'] = $this->berita->getAllBerita();
        $data['tampil_kategori'] = $this->berita->getAllKategori();
        $data['tampil_wartawan'] = $this->berita->getAllWartawan();
        $this->form_validation->set_rules('judul_berita', 'Judul Berita', 'required');
        $this->form_validation->set_rules('isi_berita', 'Isi Berita', 'required');
        if (empty($_FILES['gambar_berita']['name'])) {
            $this->form_validation->set_rules('gambar_berita', 'Gambar Berita', 'required');
        }
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('ui/Header');
            $this->load->view('pages/Berita', $data);
            $this->load->view('ui/Footer');
        } else {
            $config['upload_path'] = './images/berita';
            $config['allowed_types'] = "gif|jpg|png|jpeg";
            $config['max_size'] = '2000'; // max size in KB
            $config['encrypt_name'] = TRUE;
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if (!$this->upload->do_upload('gambar_berita')) {
                $error = array('error' => $this->upload->display_errors());
                $this->session->set_flashdata('error', $error['error']);
                $this->load->view('ui/Header');
                $this->load->view('pages/Berita', $data);
                $this->load->view('ui/Footer');
            } else {
                $info = $this->upload->data();
                $image_path = $info['raw_name'] . $info['file_ext'];
                $data = [
                    "id_wartawan" => $this->input->post('wartawan_berita'),
                    "id_kategori" => $this->input->post('kategori_berita'),
                    "judul_berita" => $this->input->post('judul_berita'),
                    "isi_berita" => $this->input->post('isi_berita'),
                    "gambar_berita" => $image_path,
                    "tanggal_berita" => date('Y-m-d'),
                    "status_berita" => "false",
                    "ket_berita" => $this->input->post('ket_berita'),
                ];
                $addBerita = $this->berita->addBerita($data);
                if ($addBerita) {
                    redirect('pages/Berita', 'refresh');
                } else {
                    redirect('gagal', 'refresh');
                }
            }
        }
    }
    public function delete($id)
    {
        $this->berita->deleteBerita($id);
        redirect('pages/Berita', "refresh");
    }
    public function update($id)
    {
        $data['judul'] = "Update Berita";
        $data['tampil_berita'] = $this->berita->getBeritaById($id);
        $data['tampil_kategori'] = $this->berita->getAllKategori();
        $data['tampil_wartawan'] = $this->berita->getAllWartawan();
        $this->form_validation->set_rules('judul_berita', 'Judul Berita', 'required');
        $this->form_validation->set_rules('isi_berita', 'Isi Berita', 'required');
        if (empty($_FILES['gambar_berita']['name'])) {
            $this->form_validation->set_rules('gambar_berita', 'Gambar Berita', 'required');
        }
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('ui/Header');
            $this->load->view('pages/update/Berita_update', $data);
            $this->load->view('ui/Footer');
        } else {
            $config['upload_path'] = './images/berita';
            $config['allowed_types'] = "gif|jpg|png|jpeg";
            $config['max_size'] = '2000'; // max size in KB
            $config['encrypt_name'] = TRUE;
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if (!$this->upload->do_upload('gambar_berita')) {
                $error = array('error' => $this->upload->display_errors());
                $this->session->set_flashdata('error', $error['error']);
                $this->load->view('ui/Header');
                $this->load->view('pages/update/Berita_update', $data);
                $this->load->view('ui/Footer');
            } else {
                $id_berita = $this->input->post('id_berita');
                $info = $this->upload->data();
                $image_path = $info['raw_name'] . $info['file_ext'];
                $data = [
                    "id_wartawan" => $this->input->post('wartawan_berita'),
                    "id_kategori" => $this->input->post('kategori_berita'),
                    "judul_berita" => $this->input->post('judul_berita'),
                    "isi_berita" => $this->input->post('isi_berita'),
                    "gambar_berita" => $image_path,
                    "ket_berita" => $this->input->post('ket_berita'),
                ];
                $updateBerita = $this->berita->updateBerita($data, $id_berita);
                if ($updateBerita) {
                    redirect('pages/Berita', 'refresh');
                } else {
                    redirect('gagal', 'refresh');
                }
            }
        }
    }
}

9
  • id you definded method in MODEL, then in any controller you can use . first load particular model and then call model method from the controller. Commented Sep 12, 2019 at 4:20
  • @DevsiOdedra sorry, can i get simple example ? Commented Sep 12, 2019 at 4:24
  • you can call model from controller not from model to model with CI 3. Commented Sep 12, 2019 at 4:30
  • @DevsiOdedra oops i think i miss write Controller to Model, I Edited it. Sorry Commented Sep 12, 2019 at 4:39
  • you have to return data from tampil_halaman_berita and pass in view of your index, function call is ok but you are not passing data to view. Commented Sep 12, 2019 at 4:43

2 Answers 2

1

Here, you need to use PASS BY REFERENCE with & concept to fix your issue

public function tampil_halaman_berita(&$data)//changes
    {
        $data['tampil_berita'] = $this->berita->getAllBerita();
        $data['tampil_kategori'] = $this->berita->getAllKategori();
        $data['tampil_wartawan'] = $this->berita->getAllWartawan();
    }
    public function index()
    {
        $data = array();
        $data['judul'] = "Halaman Berita";
        $this->tampil_halaman_berita($data);//changes
        echo '<pre>';print_r($data);die;
        $this->load->view('ui/Header');
        $this->load->view('pages/Berita', $data);
        $this->load->view('ui/Footer');
    }

Try my code to test, let me know what did you see?

Sign up to request clarification or add additional context in comments.

2 Comments

It's work ! can i know what for & in function ? it's for Global Definition?
This is only for controller level, if you want to use this function in all controllers or model then you need to define it in helper
0
-First load model in your tampil_halaman_berita() method
-get data using Model and return data
-then call first method to another method

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.