为了账号安全,请及时绑定邮箱和手机立即绑定

Codeigniter :Controller 内部调用函数

Codeigniter :Controller 内部调用函数

PHP
阿波罗的战车 2022-01-08 14:42:13
我在 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: NoticeMessage: Undefined variable: tampil_beritaFilename: pages/Berita.phpLine Number: 17Backtrace:File: C:\xampp\htdocs\flutter-news\application\views\pages\Berita.phpLine: 17Function: _error_handlerFile: C:\xampp\htdocs\flutter-news\application\controllers\pages\Berita.phpLine: 21Function: viewFile: C:\xampp\htdocs\flutter-news\index.phpLine: 315Function: 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();}我希望你们都明白我的问题。
查看完整描述

2 回答

?
神不在的星期二

TA贡献1963条经验 获得超6个赞

在这里,您需要使用PASS BY REFERENCEwith &concept 来解决您的问题


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

    }

试试我的代码来测试,让我知道你看到了什么?


查看完整回答
反对 回复 2022-01-08
?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

-First load model in your tampil_halaman_berita() method

-get data using Model and return data

-then call first method to another method


查看完整回答
反对 回复 2022-01-08
  • 2 回答
  • 0 关注
  • 137 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信