Home Articles

将数据添加到codeigniter子视图 - 返回错误

Asked
Viewed 930 times
0

我正在使用CodeIgniter文件上传类 .

我无法将错误数组加载到文件上载表单视图中 . (这会返回'未定义的变量:错误'消息,或'数组到字符串转换'错误消息)

我也无法将upload_data数组加载到成功视图 . (这会返回'数组到字符串转换'错误'消息 . )

这是我的控制器 - upload.php

<?php

class Upload extends  Admin_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));

        $this->load->helper('directory');
     }

    function index()
    {

        $this->data['subview'] = 'admin/upload_form';
        $this->load->view('admin/_layout_main', $this->data);

    }

    function do_upload()
    {


        $this->load->library('upload');

        if ( ! $this->upload->do_upload())
        {

            $error = array('error' => $this->upload->display_errors());

            $this->data['error'] = $error;


            $this->data['subview'] = 'admin/upload_form';
            $this->load->view('admin/_layout_main', $this->data);


         }
         else
         {
            $upload_data = array('upload_data' => $this->upload->data());

            $this->data['upload_data'] = $upload_data;

            $this->data['subview'] = 'admin/upload_success' ;


            $this->load->view('admin/_layout_main', $this->data);
         }
      }
  }

  ?>

这是上传表格视图upload_form.php

<?php  echo $error;?>

<?php echo form_open_multipart('admin/upload/do_upload');?>

<input type="file" name="userfile" size="20" />



<input type="submit" value="upload" /> </form>

这是成功的页面视图

<ul>
  <?php foreach ($upload_data as $item => $value):?>
  <li><?php echo $item;?>: <?php echo $value;?></li>
      <?php endforeach; ?>
</ul>

 <p><?php echo anchor('admin/upload', 'Upload Another File!'); ?></p>

1 Answer

  • 1
    $upload_data = array('upload_data' => $this->upload->data());
    $this->data['upload_data'] = $upload_data;
    

    这意味着当您在视图中print_r $ upload_data时,其中有一个数组,其中包含“upload_data”键和另一个数组作为值 . 然后当你回显$ value时它返回一个错误,因为它是一个数组而不是一个字符串 .

    我相信你所寻找的只是:

    $this->data['upload_data'] = $this->upload->data();
    

Related