![]() |
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 ![]() 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( View: Code: foreach($errors as $type => $message): to produce: Code: Username: The username you supplied has already been registered. 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'); So would do this instead: Code: $data['errors'] = array(); 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) { Relevant controller code Code: $analytics_referrals = $this->Analytics_model->get_analytics_referrals('2010-12-22', '2011-01-21'); Relevant view code: Code: 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 ![]() 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(); 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 ( then in view use Code: foreach ($array_name as $item => $value): It seems to work ![]() |