Welcome Guest, Not a member yet? Register   Sign In
Problem with $this->load->vars($data);
#1

[eluser]behnampmdg3[/eluser]
Hello;

The simple script below uploads photos and successfully sends the "upload success" message to view.

But on upload failure it doesn't send the message ($data) to the view which is very strange!

Because I can print_r($data) in controller and see the error message there! So how is it that I can see the variable in the controller but not in the view?

Thanks

Controller
Code:
class Gallery extends CI_Controller
{

public function index()
  {
   $data['title'] = "Gallary Page";
   $this->load->vars($data);
   if($this->input->post('submit'))
    {
     $this->do_upload();
    }
   $this->load_photos();
   $this->view_things();
  }

function do_upload()
  {
   $config['upload_path'] = 'uploads/';
   $config['allowed_types'] = 'gif|jpg|png';
   $config['max_size'] = '1000';
   $config['max_width']  = '1024';
   $config['max_height']  = '768';
   $this->load->library('upload', $config);
  
   if (!$this->upload->do_upload())
    {
     $data = array('upload_data' => $this->upload->display_errors());
     $this->load->vars($data);
    }
   else
    {
     $data = array('upload_data' => $this->upload->data());
     $this->load->vars($data);
    }
  
  }

public function load_photos()
  {
   $this->load->model('load_photos_model');
   $photos = $this->load_photos_model->check();
   if($photos)
    {
     foreach($result as $row)
      {
       $data['photos'][]=$row->photo;
      }
     return true;
    }
   else
    {
     $data['photos_message'] = "There are no photos uploaded yet!";
     $this->load->vars($data);
    }
  }

  public function view_things()
  {
   $this->load->view('header_view');
   $this->load->view('gallery_view');
   $this->load->view('footer_view');
  }
}

View:
Code:
<div id="common_div">
Gallery<br />
&lt;?php echo $photos_message;?&gt;
<ul>
&lt;?php foreach ($upload_data as $item => $value):?&gt;
<li>&lt;?php echo $item;?&gt;: &lt;?php echo $value;?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>
&lt;form action ="&lt;?php echo site_url();?&gt;gallery" method="post" enctype="multipart/form-data"&gt;
<table>
  <tr>
   <td>File</td>
   <td>&lt;input class = "text-box" type="file" name="userfile" value="&lt;?php echo set_value('email'); ?&gt;"/&gt;&lt;/td>
  </tr>
  <tr>
   <td colspan="2">&lt;input type="submit" value="Upload" name="submit" class="input_submit"/&gt;&lt;/td>
  </tr>
</table>
&lt;/form&gt;
</div>

On successful upload I get:
Quote: file_name: my_photo1.jpg
file_type: image/jpeg
file_path: C:/wamp/www/flatmates/uploads/
full_path: C:/wamp/www/flatmates/uploads/my_photo1.jpg
raw_name: my_photo1
orig_name: my_photo.jpg
client_name: detailed listing (2).jpg
file_ext: .jpg
file_size: 72.41
is_image: 1
image_width: 641
image_height: 768
image_type: jpeg
image_size_str: width="641" height="768"

On failure upload I get:
Quote: A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/gallery_view.php

Line Number: 5
#2

[eluser]NeoArc[/eluser]
$this->upload->display_errors() returns a string Sad

http://ellislab.com/codeigniter/user-gui...ading.html




Theme © iAndrew 2016 - Forum software by © MyBB