CodeIgniter Forums
Trouble passing array to view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Trouble passing array to view (/showthread.php?tid=13056)

Pages: 1 2


Trouble passing array to view - El Forum - 11-09-2008

[eluser]LinkFox[/eluser]
Hi,

I have an array created called $errors like so

$errors = array();

I then perform some some validation, if fields are incorrect the view is loaded and $errors is passed to the view like so

$this->load->view('registation_page', $errors);

However, $errors isn't seen by registation_page?

What am I doing wrong?

Thanks Wink


Trouble passing array to view - El Forum - 11-09-2008

[eluser]iainco[/eluser]
Can you show us more code please?

Try using PHP's "print_r" function to print out the content of arrays.

The array needs to be setup like this:

Controller:
Code:
$errors = array(
'Username' => 'The username you supplied has already been registered.',
'Password' => 'Your password fields did not match.'
)

$this->load->view('view', $errors);

View:
Code:
foreach($errors as $type => $message):
&lt;?=$type?&gt;: &lt;?=$message?&gt; <br />
endforeach;

to produce:
Code:
Username: The username you supplied has already been registered.
Password: The password fields did not match.

But you should really take a look at the Form Validation library in the user guide.


Trouble passing array to view - El Forum - 11-09-2008

[eluser]Pascal Kriete[/eluser]
[Edit: What chels said ... 8-/ ]

The array you pass to the array is extacted, so the array items turn into variables.

Basically:
Code:
$test = array('a' => 'b');

$this->load->view('test', $test);

// in the view
echo $a //prints b

So would do this instead:
Code:
$data['errors'] = array();
$this->load->view('registration_page', $data);

Welcome to CodeIgniter.

ps. have you considered the native form validation class?


Trouble passing array to view - El Forum - 02-10-2011

[eluser]Ziggomat1k[/eluser]
I am having a similar issue. I am trying to pass an array to the view, and no matter what the view tells me the variable is undefined.

If I print_r the array in the controller, it looks like this:

Code:
Array ( [site] => 8944 [(direct)] => 2160 [site] => 878 [site] => 696 [site] => 660 [site] => 321 [site] => 167 [site] => 155 [site] => 141 [site] => 132 [site] => 123 [site] => 120 [site] => 116 [site] => 88 [site] => 88 [site] => 82 [site] => 73 [site] => 65 [site] => 45 [site] => 39 )

Is this in the wrong format?

Relevant model code:
Code:
foreach($ga->getResults() as $result) {

          $visits = $result->getVisits();
          $source = $result->getSource();

         $analytics_referrals[$source] = $visits;
     }


     return $analytics_referrals;

Relevant controller code
Code:
$analytics_referrals = $this->Analytics_model->get_analytics_referrals('2010-12-22', '2011-01-21');

                print_r($analytics_referrals);

                $this->load->view('google_analytics_view', $analytics_referrals);

Relevant view code:
Code:
print "<table>";
foreach ($analytics_referrals as $source => $visits)
{
    print "<tr>";
    print "<td>" . $source . "</td><td>" . $visits . "</td>";
    print "</tr>";

}
print "</table>";



Trouble passing array to view - El Forum - 02-10-2011

[eluser]LinkFox[/eluser]
Hey you need to pass the data in an array.

So in your case do the following

$data['analytics_referrals'] = $analytics_referrals;

then $this->load->view('google_analytics_view', $data);

You can then access the data in your view using the code you quoted above.

Hope this helps.

Regards

David

edit: meant pass $data to your view.


Trouble passing array to view - El Forum - 02-10-2011

[eluser]LinkFox[/eluser]
And wow I posted this originally over 2 years ago Tongue Time flies!


Trouble passing array to view - El Forum - 02-10-2011

[eluser]Ziggomat1k[/eluser]
Thanks man. I did pass the data though - I think it has something to do with not being able to pass an associative array. You have to use some kind of multi-dimensional syntax. I'm trying to figure out how that works.


Trouble passing array to view - El Forum - 02-10-2011

[eluser]Ziggomat1k[/eluser]
Ok - I've narrowed it down to this:
Code:
$analytics_referrals = array();

     $analytics_referrals['data'] = array ();

     foreach($ga->getResults() as $result) {

          $analytics_referrals['data'] = array (
          'source' => $result->getSource(),
          'visits' => $result->getVisits()
          );
     }
return $analytics_referrals;

The Codeigniter view is happy with the code - but is only displaying one result. I think this is because I am overwriting this array each time I loop. Can anyone recommend the right syntax to load up my results into this array?


Trouble passing array to view - El Forum - 02-10-2011

[eluser]Ziggomat1k[/eluser]
THE TRICK IS THIS:

I may have the terminology wrong here - but when you load your data in the controller, give the variable you store the data in a name in brackets, as in:

$your_variable_storing_your_data_array['name_you_want_to_use_to_access_your_data_in_the_view']


THEN - run your foreach loop on that name - NOT on the name of the array.

It's late. I hope that makes sense and I hope it helps someone else down the road.


Trouble passing array to view - El Forum - 06-29-2012

[eluser]cyberjunkie[/eluser]
I found this post looking for a way to pass an array to the view. None of the solutions worked so I tried:

Code:
$data['array_name'] = array (
'1' => 'apple',
'2' => 'pineapple'
);

$this->load->view('file', $data);

then in view use

Code:
foreach ($array_name as $item => $value):

It seems to work Smile