-2

I need to load a view into a view within CodeIgniter, but cant seem to get it to work.

I have a loop. I need to place that loop within multiple views (same data different pages). So I have the loop by itself, as a view, to receive the array from the controller and display the data.

But the issue is the array is not available to the second view, its empty

The second view loads fine, but the array $due_check_data is empty

SO, I've tried many things, but according to the docs I can do something like this:

Controller:

// gather data for view

$view_data = array(
   'loop' => $this->load->view('checks/include/due_checks_table', $due_check_data, TRUE),
   'check_cats' => $this->check_model->get_check_cats(),
   'page_title' => 'Due Checks & Tests'
);

$this->load->view('checks/due_checks',$view_data);

But the array variable $due_check_data is empty

I'm just getting this error, saying the variable is empty?

Message: Undefined variable: due_check_data

frobak
  • 557
  • 2
  • 11
  • 30

4 Answers4

1

You are passing the $view_data array to your view. Then, in your view, you can access only the variables contained in $view_data:

  • $loop
  • $check_cats
  • $page_title

There is no variable due_check_data in the view.


EDIT

The first view is contained in the variable $loop, so you can just print it in the second view (checks/due_checks):

echo $loop;

If you really want to have the $due_check_data array in the second view, why don't you simply pass it?

$view_data = array(
   'loop' => $this->load->view('checks/include/due_checks_table', $due_check_data, TRUE),
   'check_cats' => $this->check_model->get_check_cats(),
   'page_title' => 'Due Checks & Tests',
   'due_check_data' => $due_check_data
);

$this->load->view('checks/due_checks',$view_data);
Dacklf
  • 700
  • 4
  • 15
0

You are missing <?php. Should be something like this

<?php echo $due_check_data; ?>
muya.dev
  • 966
  • 1
  • 13
  • 34
0

Controller seems has no error. Check out some notices yourself:

<?=$due_check_data?>

This only available in PHP >= 5.4

<? echo $due_check_data; ?>

This only available when you enable short open tag in php.ini file but not recommended

KmasterYC
  • 2,294
  • 11
  • 19
-1

OK, i managed to solve this by declaring the variables globally, so they are available to all views.

        // gather data for view
        $view_data = array(
            'due_check_data' => $combined_checks,
            'check_cats'     => $this->check_model->get_check_cats(),
            'page_title'     => 'Due Checks & Tests'
        );

        $this->load->vars($view_data);
        $this->load->view('checks/due_checks');
frobak
  • 557
  • 2
  • 11
  • 30