foreach question. - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: foreach question. (/showthread.php?tid=67834) |
foreach question. - wolfgang1983 - 04-16-2017 I have a list of my $positions = array() in array below PHP Code: $positions = array( In my foreach loop it gets the positions that are set in the database and then loads the view for it. But because a position may not be set in database it throws error like Undefined variable: column_right How can I make it not throw that type of error if can not find position then should be some thing like PHP Code: $data['column_right'] = $this->load->view('column_right', '', TRUE); PHP Code: <?php RE: foreach question. - PaulD - 04-16-2017 If I understand you correctly, you need to run through all your possible positions to check they are set, and set a default view of some sort if they are not (ie they were missing from the database). In your main layout view you could add something like: PHP Code: <?php if (!empty($column_left)) echo $column_left; Or in your controller do it like this PHP Code: foreach ($positions as $position) { ...to make sure any missing columns are loaded with a default view of some sort. However, you seem to be hardcoding for a two column page layout. What if the page layout requires three columns, or four columns? It may be that you hardcoded the page layout just for your question, but it might be better to have a table that holds the layout configuration for the page, something like: Code: page_section_id | page_section_for_page_id | page_section_name | page_section_sort_order Then your pages could have different block layouts. Best wishes, Paul. RE: foreach question. - wolfgang1983 - 04-16-2017 (04-16-2017, 03:49 AM)PaulD Wrote: If I understand you correctly, you need to run through all your possible positions to check they are set, and set a default view of some sort if they are not (ie they were missing from the database). Thank you will update you on how I go |