Question

I'm passing data from my controller:

$data = $this->post->get_post($postID);
$data['errors'];
$this->load->view('register_user.php', $data);

But For some reason, when trying to extract the array in the view, like so:

extract($data);
foreach ($data as $result)
{
echo $result,'<br>';
}
endforeach;

I get $data is null error.

What's the reason for the null array?

A couple things:

  1. 'post' is a model which i construct into the class,which pulls a certain row in a certain table that contains all of the details for a specific posts. When trying to echo the array in the controller, it shows.

  2. could inserting a new key and value into the array ('errors'=> 0 ) the cause for the error?

Was it helpful?

Solution

You fetch your data in view like this.

$errors

Whatever you put in $data variable array and you pass $data variable to view, is "converted" in a such way that every element (index) is a variable in view.

So in controller we have $data['news'] = array(); $data['errors'] = array(); But in views we have only 2 variables that we can work with $news and $errors.

please make adjustment to your code as follows

$data['post'] = $this->post->get_post($postID);
var_dump($data['post']);
$this->load->view('register_user.php', $data);

and in view

foreach ($post as $result)...

OTHER TIPS

You can access your data with array keys directly in view

For example:

if 
    $data = array(
        'test1' => 'test1_data',
        'test2' => 'test1_data'
    );

In this case you can access $test1 directly like

<?php echo $test1; ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top