• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing and accessing values within data between controller and view

#1
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.
Reply

#2
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.
Reply

#3
Do you load the view correctly?

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

http://www.codeigniter.com/userguide3/ge...o-the-view
Reply

#4
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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.