Codeigniter :Controller 内部调用函数

我在 Codeigniter 的另一个函数中有问题调用函数。所以我有2个功能:


功能 1 有控制器用于从数据库中获取数据:

public function tampil_halaman_berita()

    {

        $data['tampil_berita'] = $this->berita->getAllBerita();

        $data['tampil_kategori'] = $this->berita->getAllKategori();

        $data['tampil_wartawan'] = $this->berita->getAllWartawan();

    }

函数 2 从函数 1 调用控制器来索引:

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

    }

但是我收到了这个错误,我的函数tampil_halaman_berita没有调用到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

我不想,像这样对我的所有函数重复每个模型,所以我创建了一些函数,我只是调用函数名。


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();

}

我希望你们都明白我的问题。


阿波罗的战车
浏览 138回答 2
2回答

神不在的星期二

在这里,您需要使用PASS BY REFERENCEwith &concept 来解决您的问题public function tampil_halaman_berita(&$data)//changes&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $data['tampil_berita'] = $this->berita->getAllBerita();&nbsp; &nbsp; &nbsp; &nbsp; $data['tampil_kategori'] = $this->berita->getAllKategori();&nbsp; &nbsp; &nbsp; &nbsp; $data['tampil_wartawan'] = $this->berita->getAllWartawan();&nbsp; &nbsp; }&nbsp; &nbsp; public function index()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $data = array();&nbsp; &nbsp; &nbsp; &nbsp; $data['judul'] = "Halaman Berita";&nbsp; &nbsp; &nbsp; &nbsp; $this->tampil_halaman_berita($data);//changes&nbsp; &nbsp; &nbsp; &nbsp; echo '<pre>';print_r($data);die;&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('ui/Header');&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('pages/Berita', $data);&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('ui/Footer');&nbsp; &nbsp; }试试我的代码来测试,让我知道你看到了什么?

慕无忌1623718

-First load model in your tampil_halaman_berita() method-get data using Model and return data-then call first method to another method
打开App,查看更多内容
随时随地看视频慕课网APP