CodeIgniter将多个变量加载到视图中

时间:2016-01-12 15:34:59

标签: php codeigniter

我有一个页面在一个部分和另一个部分中有分页,有一个用户可以上传文件的多部分表单。目前问题是如何加载视图?文件上传要求我加载错误变量,而分页需要数据变量。我该如何解决这个问题?谢谢。

这是我的控制器代码:

function index()
{   

    $username = $this->session->userdata('username');
    $company = $this->session->userdata('company');
    $this->load->library('pagination');
    $this->load->model('announcement');

    $config = array();
    $config['base_url']=site_url('announcements/index');
    $config['total_rows']=$this->announcement->count($username,$company);
    $config['per_page']=5;
    $this->pagination->initialize($config);
    $data['announcement'] = $this->announcement->fetch_announcement($username,$company,$config['per_page'],$this->uri->segment(3));
    $data['links'] = $this->pagination->create_links(); 



    $this->load->view('includes/admin_header');
    $this->load->view('announcements',$data);
    $this->load->view('includes/admin_footer');
}

function upload()
{
    //the name on the view must be userfile
    $username = $this->session->userdata('username');
    $company = $this->session->userdata('company');
    $title = $this->input->post('title');
    $description =$this->input->post('description');
    $path = './assets/files/'.$company.'/announcements';
    $config['upload_path'] = $path;
    $config['allowed_types'] = 'pdf';
    $config['max_size']    = '10000';
    $this->load->library('upload',$config);

    $data = array(
            'title'=>$this->input->post('title'),
            'description'=>$this->input->post('description'),
            'addedby'=>$username,
            'company'=>$company,
            'dateadded'=>date('Y-m-d'),
            'published'=>'No',
            'path'=>$path
    );

    $this->load->model('announcement');
    $this->load->library('pagination');
    $config['per_page']=5;
    if(!$this->upload->do_upload())
    {

        //$error = array('error'=>$this->upload->display_errors());
        $data = array(
        'announcement' => $this->announcement->fetch_announcement($username,$company,$config['per_page'],$this->uri->segment(3)),
        'links' => $this->pagination->create_links(), 
        'error' => $this->upload->display_errors()
        );
        $this->load->view('includes/admin_header');
        $this->load->view('announcements',$data);
        $this->load->view('includes/admin_footer');
    }
    else
    {
        $data = array(
        'announcement' => $this->announcement->fetch_announcement($username,$company,$config['per_page'],$this->uri->segment(3)),
        'links' => $this->pagination->create_links(), 
        'error' => $this->upload->display_errors()
        );
        $file_data = array('upload_data' => $this->upload->data());
        $result = $this->announcement->create($insert);
        $this->load->view('includes/admin_header');
        $this->load->view('announcements',$data);
        $this->load->view('includes/admin_footer');
    }

}

1 个答案:

答案 0 :(得分:2)

此:

$error = array('error'=>$this->upload->display_errors());
$this->load->view('includes/admin_header');
$this->load->view('announcements',$error);

与此相同:

$data = array('error'=>$this->upload->display_errors());
$this->load->view('includes/admin_header');
$this->load->view('announcements',$data);

变量的命名方式无关紧要。它必须是一个数组并且具有error键。

因此,总而言之,您可以拥有:

$data = array(
    'announcement' => $this->announcement->fetch_announcement($username,$company,$config['per_page'],$this->uri->segment(3)),
    'links' => $this->pagination->create_links(), 
    'error' => $this->upload->display_errors()
);
$this->load->view('announcements',$data);