dr637349 2013-10-14 12:18
浏览 38
已采纳

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

I'm using CodeIgniter file upload class.

I am unable to load the error array into the file upload form view. (this returns a 'Undefined variable: error' message, or a ' Array to string conversion' error message)

and I am also unable to load the upload_data array to the success view. (this returns a ' Array to string conversion' error' message.)

This is my controller - 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);
         }
      }
  }

  ?>

This is up upload form view upload_form.php

<?php  echo $error;?>

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

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

<br /><br />

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

</form>

This is the success page view

 <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条回答 默认 最新

  • doulian1852 2013-10-14 12:28
    关注
    $upload_data = array('upload_data' => $this->upload->data());
    $this->data['upload_data'] = $upload_data;
    

    This means that when you print_r $upload_data in a view, that there is an array in there with a key of 'upload_data' and another array as a value. Then when you echo $value it returns an error because it's an array not a string.

    I believe what you are looking for is just:

    $this->data['upload_data'] = $this->upload->data();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)