35

I want to pass $data from the controller named poll to the results_view however I am getting an undefined variable error.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Poll extends CI_Controller {

    public function __construct()
       {
            parent::__construct();
            $this->load->database();
            $this->load->helper('form');
       }

    public function index()
    {

        $this->load->view('poll_view',$data);
    }

    public function vote()
    {
        echo "Voting Successfull";
        $this->db->insert('votes',$_POST);
    }

    public function results()
    {
        echo "These are the results";
        //$query = $this->db->get('votes');
        $data = "hello";
        $this->load->view('results_view', $data);

    }
}

Results_view.php

<html>
<?php echo $data; ?>
</html>
Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79
Andrew Lynch
  • 1,297
  • 3
  • 14
  • 25

13 Answers13

51

$data should be an array or an object: http://codeigniter.com/user_guide/general/views.html

$data = array(
    'title' => 'My Title',
    'heading' => 'My Heading',
    'message' => 'My Message'
);

$this->load->view('results_view', $data);

results_view.php

<html>
<?php 
//Access them like so
echo $title.$heading.$message; ?>
</html>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • i have read the article about how to pass data From Controller To View In CodeIgniter http://www.cloudways.com/blog/how-to-pass-data-in-codeigniter/ – Owais Alam Aug 21 '17 at 11:59
  • @Lawrence I tried your code, which is working after clicked but before If I refreshed the view page then I am getting Undefined variable: Any help? – Naren Verma Oct 06 '17 at 09:23
  • @NarendraVerma you're probably using a $_GET param somewhere and not checking its set before using. – Lawrence Cherone Oct 06 '17 at 13:26
22

In simple terms,

$data['a'] in controller becomes $a in your view. ($data won't exist in your view, only the index will become available)

e.g.

Controller:    
$data['hello']='hellow world';

view:
echo $hello;
itachi
  • 6,323
  • 3
  • 30
  • 40
  • Yes, you right. $data doesn't exist on view directly. Best way to pass the value on view it's by $data['hello'] – heySushil Mar 05 '19 at 07:49
7

You just need to create a array, you using codeigniter right?

Example on controller:

$data['hello'] = "Hello, world";
$this->load->view('results_view', $data);

In de page "results_view" you just have to:

<?php echo $hello;?>

Obs: You can create n datas, just pay attention in the name and make it a array.

Obs²: To use the data use the key of the array with a echo.

Space
  • 83
  • 1
  • 3
  • I tried your code, which is working after clicked but before If I refreshed the page I am getting Undefined variable: hello. Any help? – Naren Verma Oct 06 '17 at 08:55
6

The view wouldn't call the data 'data'

The controller would include an associative index (not sure if that's correct nomenclature) for data e.g 'stuff' looking thus $data['stuff']

You'd echo in the view so: echo $stuff; not echo $data;

I am a v lowly code fiddler but do really like CodeIgniter so excuse me if i've got this arse about tit.

One more thing - surely your constructor function is a bit of a waste. All that loading of libraries and helpers is done with the autoload file.

Tom
  • 173
  • 2
  • 12
4

You can create property $data = []; inside CI_Controller(path: system/core/Controller.php) and store all data to show in view. U can load common data like languages, menu, etc in CI_Controller. Also u can add special data for view in controller. (example: $this->data['message'] = "Hello world";) Finally, u can pass $this->data to view when load view (example: $this->load->view('view_name',$this->data);)

I hope this will help you

2

you can do it this way

defined array in controller

$data['hello'] = "hello";

and pass variable to view

echo $hello; 
denny
  • 2,084
  • 2
  • 15
  • 19
noushad mohammed
  • 375
  • 1
  • 4
  • 20
2

If you pass

$data = your code
$this->load->view('your-page', $data);

and get data on your view as

<?php echo $data;?>

It won't work because ci didn't understand this patern. If like to pass value form controller to view so you can try this -

controller -

$data['any-name'] = your values;
$this->load->view('your-page', $data);

then in your view you can get this data by -

<?php echo $any-name;?>

Hope this helps you.

heySushil
  • 493
  • 7
  • 13
  • https://stackoverflow.com/a/9322340/2943403 the parts of this answer that are correct are redundant. This post can safely be removed. – mickmackusa Jun 24 '19 at 11:37
1

In your controller you can pass

$data['poll'] = "Your results";

In your view you can call

echo $poll; 
1

Ok so I finally solved it. You should really have a model (it helps a lot)

In your model do something like

Model

class poll_model extends CI_MODEL {

 function __construct() {
   $this-load->database(); 
 }

 function get_poll {
   $this->db->query("SELECT * FROM table");
   $row = $query->row();

   $obj = array(
    'id' => $row->id
  );
   return $obj;
 }
}

Now if you have more than an id say name of poll # you can add in array. Now in your controller do

class Poll extends CI_Controller {

public function __construct()
   {
        parent::__construct();
        $this->load->database();
        $this->load->helper('form');
        $this->load->model('poll_model');
   }

public function index()
{
    $data["a"] = $this->poll_model->get_poll();
    $this->load->view('poll_view',$data);
}

And finally in VIEW put

<? echo $a["id"]; ?>

This is a big help. I figured it out by testing and it works for me.

Tobias Mühl
  • 1,788
  • 1
  • 18
  • 30
1

In controller:

$data["result"] = $this->login_model->get_login(); // Get array value from DB..

$this->load->view('login-form',$data); // Pass the array to view 

In view:

print_r($result);  // print the array in view file
Blackbam
  • 17,496
  • 26
  • 97
  • 150
0

I have seen all above answer so here is what I do when I have to load the data from the controller to my view. To Pass the Data To the view from the controller:

public function your_controller(){

   // Your Necessary Code 
   // You have the $data, $data2, $data3 to post to the view.

   $this->load->view('your_view_directory or view_page',['data'=>$data, 'data2'=>$data2, 'data3'=>$data3... so on ]);

}

And At the View Side You can simply retrieve that data: To Display You can simply use echo, print, print_r. And if you want to loop over it, you can do that as well.

always-a-learner
  • 3,671
  • 10
  • 41
  • 81
0

In controller:

public function product(){

$data = array("title" => "Books", "status"=>"Read","author":"arshad","company":"3esofttech",

"subject":"computer science");

Data From Model to controller

$this->load->model('bookModel');
$result = $this->bookModel->getMoreDetailsOfBook();

**Add *$result* from model to *$data* array**  
$data['tableRows'] = $result;

$data from Controller to View

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

And to access in view file views/user.php

<?php  echo $data;
 foreach($tableRows as $row){ echo
 $row['startData']; } ?>
Arshad Syed
  • 403
  • 4
  • 9
0

Instead of

$data = "hello";
$this->load->view('results_view', $data);

Do

$data['hello'] = 'hello';
$this->load->view('results_view', $data);

In your controller file and controller will send data having hello as string to results_view and in your view file you can simply access by

echo $hello;
Ali Haider
  • 21
  • 4