使用提交函数在CodeIgniter视图中使用未定义的变量

时间:2014-02-02 02:14:24

标签: php codeigniter variables view undefined

我正在尝试使用codeigniter创建一个网站。我使用的结构只使用视图和控制器。该模型直接在控制器中。

所以,我收到了这条错误消息:

Severity: Notice

Message: Undefined variable: view

Filename: template/default.php

Line Number: 146

这是我在控制器上的代码(artikeladmin.php)

public function add()
    {
        if($this->input->post())
        {
            $judul = $this->input->post('judul');
            $kategori_id = $this->input->post('kategori_id');
            $tanggal = $this->input->post('tanggal');
            $gambar = $this->input->post('gambar');
            $published = $this->input->post('published');
            $deskripsi = $this->input->post('deskripsi');



            $config['upload_path'] ='assets/images/upload/';
            $config['allowed_types'] ='jpeg|png|gif|jpg';
            $config['max_size'] ='*';
            $config['max_width'] ='*';
            $config['max_height'] ='*';
            $this->load->library('upload', $config);

            if(!$this->upload->do_upload())
            {
                $data['error'] = array('error' =>$this->upload->display_errors());
                $this->load->view('admin/template/default', $data);
            }
            else
            {
            $attr = $this->upload->data();
            $args = array(
                'gambar'    => $attr['file_name'],
                'kategori_id'   => $kategori_id,
                'judul'     => $judul,
                'published' => $published,
                'tanggal'   => $tanggal,
                'deskripsi' => $deskripsi

        );

            $this->db->insert('artikel', $args);            
            $this->session->set_flashdata('message','Data berhasil disimpan');
            redirect('artikeladmin');
        }

        }
        $user = $this->session->userdata('myuser');
        if(!isset($user) or empty($user))
        redirect('login');

        $sql = " SELECT * FROM kategori";
        $query = $this->db->query($sql);
        $kategori = $query->result_array();

        $data['view'] = 'admin/add_article';
        $data['kategori'] = $kategori;
        $data['action'] = 'artikeladmin/add/';
        $this->load->view('admin/template/default', $data);
    }

这是我的观点(template / default.php)

<div id="content" class="span10">
         <?php $this->load->view($view); ?> 
        </div>

在我的控制器中你可以看到我已经定义了像这样的变量视图

$data['view'] = 'admin/add_article';

我试图添加像

这样的新变量

$data['view'] = $data;

但错误仍然发生

1 个答案:

答案 0 :(得分:2)

如果do_upload()失败,则会在未设置$view变量的情况下加载视图:

if(!$this->upload->do_upload())
{
    $data['error'] = array('error' =>$this->upload->display_errors());
    $this->load->view('admin/template/default', $data);
}

default.php查找$view

<?php $this->load->view($view); ?> 

你得到Undefined变量错误。

在调用$view之前尝试实例化default.php