Welcome Guest, Not a member yet? Register   Sign In
Passing and accessing values within data between controller and view

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.

What do you have in accounts_model->get_accounts() ?
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.

Do you load the view correctly?

PHP Code:


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.

Theme © iAndrew 2016 - Forum software by © MyBB