CodeIgniter Forums

Full Version: foreach question.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a list of my $positions = array() in array below


PHP Code:
$positions = array(
'column_left',
'column_right',
'content_top',
'content_bottom'
);

foreach (
$tmpdata as $position => $module) {
if (
in_array($position$positions)) {
$data[$position] = $this->load->view($position$tmpdata[$position], TRUE);
}



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

class Welcome extends CI_Controller {

public function 
__construct() {
parent::__construct();
$this->load->model('design/layout_model');
}

public function 
index() {
$layout_id $this->layout_model->getlayoutID($this->router->class);

$modules $this->layout_model->getlayoutsmodule($layout_id);

$tmpdata = array();

foreach (
$modules as $module) {

$name $module['name'];
   
   $this
->load->library('module/' $name);
   
   $tmpdata
[$module['position']]['modules'][] = $this->load->view('module/question_help'$this->$name->get(), TRUE);
}

$positions = array(
'column_left',
'column_right',
'content_top',
'content_bottom'
);

foreach (
$tmpdata as $position => $module) {
if (
in_array($position$positions)) {
    $data[$position] = $this->load->view($position$tmpdata[$position], TRUE);
}
}

$this->load->view('welcome_message'$data);
}

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;
   else echo 
'default code for column left if it is missing';
?>

Or in your controller do it like this
PHP Code:
foreach ($positions as $position) { 
 if (!isset(
$data[$position])) {
 
// load default for that position
 
$data['position'] = $this->load->view('default_views/'.$position''TRUE);
 }


...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
----------------|--------------------------|-------------------|-------------------------
     1         |          1               |    header         |         1
     2         |          1               |    left_column    |         2
     3         |          1               |    right_column   |         3
     4         |          1               |    footer         |         4
     5         |          2               |    header         |         1
     6         |          2               |    page           |         2
     7         |          2               |    footer         |         3
     8         |          3               |    header         |         4
     9         |          3               |    left_column    |         1
     10        |          3               |    middle_column  |         2  
     6         |          3               |    right_column   |         3
     7         |          3               |    footer         |         4

Then your pages could have different block layouts.

Best wishes,

Paul.
(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).

In your main layout view you could add something like:
PHP Code:
<?php if (!empty($column_left)) echo $column_left;
 
  else echo 'default code for column left if it is missing';
?>

Or in your controller do it like this
PHP Code:
foreach ($positions as $position) { 
 if (!isset(
$data[$position])) {
 
// load default for that position
 
$data['position'] = $this->load->view('default_views/'.$position''TRUE);
 }


...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
----------------|--------------------------|-------------------|-------------------------
     1         |          1               |    header         |         1
     2         |          1               |    left_column    |         2
     3         |          1               |    right_column   |         3
     4         |          1               |    footer         |         4
     5         |          2               |    header         |         1
     6         |          2               |    page           |         2
     7         |          2               |    footer         |         3
     8         |          3               |    header         |         4
     9         |          3               |    left_column    |         1
     10        |          3               |    middle_column  |         2  
     6         |          3               |    right_column   |         3
     7         |          3               |    footer         |         4

Then your pages could have different block layouts.

Best wishes,

Paul.

Thank you will update you on how I go