CodeIgniter Forums
Passing and accessing values within data between controller and view - 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: Passing and accessing values within data between controller and view (/showthread.php?tid=61383)



Passing and accessing values within data between controller and view - Shawn - 04-11-2015

The tutorial has the following code to retrieve an array result from the database

       $data['news'] = $this->news_model->get_news();

And in the view the array result is accessed with

<?php foreach ($news as $news_item): ?>

That is to say: The 'news' inside the $data array can be accessed as the variable: $news when you pass $data to the controller

For some reason I am unable to do this with my own controller/view

               // retrieve a result set from my model. Tracing thru eclipse shows that results
              // are returned from the database
$data['accounts'] = $this->accounts_model->get_accounts();
             
              // as long as I do this in my controller I can access the result set
foreach ($data['accounts'] as $row) {
echo $row['account'] . '</br>';
echo $row['ledger'] . '</br>';
echo $row['description'] . '</br>';
}

              //But this does not work
foreach ($accounts as $row) {
echo $row['account'] . '</br>';
echo $row['ledger'] . '</br>';
echo $row['description'] . '</br>';
}

And referring to $accounts in the view does not work either. In both cases $accounts is undefined.


RE: Passing and accessing values within data between controller and view - lexxtoronto - 04-11-2015

What do you have in accounts_model->get_accounts() ?


RE: Passing and accessing values within data between controller and view - silentium - 04-12-2015

Do you load the view correctly?

PHP Code:
$this->load->view('my-view'$data); 

http://www.codeigniter.com/userguide3/general/views.html#adding-dynamic-data-to-the-view


RE: Passing and accessing values within data between controller and view - Shawn - 04-14-2015

Thank you for your recommendations. It helped me to begin by loading the view in a basic way. What I was aiming for was the following:

$main_content_data['accounts'] = $this->accounts_model->get_accounts();

$this->data['main_content_view'] = $this->load->view('share/accounts_view', $main_content_data, TRUE);
$this->load->view('share/default', $this->data);

And this worked.