Codeigniter:控制器内的Call函数

时间:2019-09-12 04:12:18

标签: php codeigniter

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

功能1具有 Controller 用于从数据库中获取数据:

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

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

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

希望大家都理解我的问题。

谢谢

  

如果您需要我的完整控制器

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

2 个答案:

答案 0 :(得分:1)

  

在这里,您需要结合使用class Class_B: UIViewController,UpdateBeneficiaryProtocol { var class_a = Class_A() override func viewDidLoad() { super.viewDidLoad() class_a.myprotocolDelegate = self; } func doGetUpdateInfo(value1: String, value2: String) { print("not calling****") } } PASS BY REFERENCE概念来解决问题

&

尝试我的代码进行测试,让我知道您看到了什么?

答案 1 :(得分:0)

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