CodeIgniter Forums
Variables are not passing in view from my controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Variables are not passing in view from my controller (/showthread.php?tid=1464)



Variables are not passing in view from my controller - dragonabv - 03-11-2015

The Controller:

PHP Code:
class Upload extends CI_Controller
{
    function 
__construct()
    {
        
parent::__construct();
        
$this->load->helper(array('form''url'));
    }

        public function 
do_upload()
        {
        
$config['upload_path'] = './assets/images/uploads/promo/';
        
$config['allowed_types'] = 'jpg|png';
        
$config['max_size']    = '2000';
        
$config['max_width' '3000';
        
$config['max_height' '1768';

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

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

        }
        else
        {
            
$data['data'] = array('upload_data' => $this->upload->data());
            
$data['error'] = "";
            
$this->load->view('admin/upload_promo'$data);
        }
        }


The View:

PHP Code:
<?php 
if($data != ""){echo "Image Uploaded Sucessfuly!";}
if(
$error != ""){echo $error;}

 
?>

PHP Code:
<?php echo form_open_multipart('upload/do_upload');?>

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

<br /><br />

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

</form>

The Error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: core/Loader.php(830) : eval()'d code

Line Number: 8



What is the Problem??


RE: Variables are not passing in view from my controller - InsiteFX - 03-12-2015

Your code should be in the index function/method not the __construct()


RE: Variables are not passing in view from my controller - dragonabv - 03-12-2015

Look carefully there is no code in my __construct(). My code is in function do_upload() of upload controller


RE: Variables are not passing in view from my controller - twpmarketing - 03-12-2015

The array: $data is exploded before passing to the view.  Only the array elements are exposed to the view, which is why the reported error is: "Undefined variable:"


Code:
<?php 
// if($data != ""){echo "Image Uploaded Sucessfuly!";}
if($error != ""){echo $error;}

 ?>


Comment the line as shown above and test again.


RE: Variables are not passing in view from my controller - mwhitney - 03-12-2015

Try changing your do_upload like this:

PHP Code:
   public function do_upload()
 
   {
 
       $config['upload_path'] = './assets/images/uploads/promo/';
 
       $config['allowed_types'] = 'jpg|png';
 
       $config['max_size'   '2000';
 
       $config['max_width' '3000';
 
       $config['max_height' '1768';

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

 
       $data = array(
 
           'error' => '',
 
           'upload_data' => '',
 
       );

 
       if (! $this->upload->do_upload()) {
 
           $data['error'] = $this->upload->display_errors();
 
       } else {
 
           $data['upload_data'] = $this->upload->data();
 
       }

 
       $this->load->view('admin/upload_promo'$data);
 
   

Then, in your view:

PHP Code:
if (empty($upload_data)) {
 
   echo empty($error) ? "An unknown error occurred when attempting the upload." $error;
 else {
 
   echo "Image Uploaded Sucessfuly!";


Most of the changes are just cleanup, but, in general, I would avoid using the same variable name ($data) to reference multiple things, and I would avoid nesting arrays where it isn't necessary. Additionally, empty() or isset() help to avoid undefined variable errors when checking viewdata (though you have to be careful with empty(), especially if you pass boolean values to a view). The same warning applies to using $variable != "" instead of $variable !== "".


RE: Variables are not passing in view from my controller - madaan_tushar - 05-26-2015

Just pass $data in the view as:
$this->load->view('view_name', $data);