简体   繁体   中英

Codeigniter :Call function inside Controller

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');
                }
            }
        }
    }
}

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?

-First load model in your tampil_halaman_berita() method
-get data using Model and return data
-then call first method to another method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM