2017-04-14 83 views
0

我有一個控制器,顯示我的每個位置含量有正確的模塊位置

Array 
(
    [0] => Array 
     (
      [layout_module_id] => 1 
      [layout_id] => 1 
      [module_id] => 1 
      [position] => column_left 
      [sort_order] => 1 
     ) 

    [1] => Array 
     (
      [layout_module_id] => 2 
      [layout_id] => 1 
      [module_id] => 2 
      [position] => column_left 
      [sort_order] => 2 
     ) 

) 

上述模塊目前我只有兩個模塊設置和在列左邊的位置沒有顯示。

由於位置視圖位於foreach循環的外側,即使未設置該位置,它們也正在拾取該模塊?如圖所示。

enter image description here

enter image description here

問:我怎樣才能確保該模塊將只在其設定位置視圖顯示。

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

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

    echo '<pre>'; 
    print_r($modules); 
    echo "</pre>"; 

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

    foreach ($modules as $module) { 
     $this->load->library('module/question_help'); 
     $data['modules'][] = $this->load->view('module/question_help', $this->question_help->set(), TRUE); 
    } 

    // Position Views 
    $data['column_left'] = $this->load->view('column_left', $data, TRUE); 
    $data['column_right'] = $this->load->view('column_right', $data, TRUE); 
    $data['content_top'] = $this->load->view('content_top', $data, TRUE); 
    $data['content_bottom'] = $this->load->view('content_bottom', $data, TRUE); 

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

public function getlayoutsmodule($layout_id) { 
    $this->db->select('*'); 
    $this->db->from('layouts_module'); 
    $this->db->where('layout_id', $layout_id); 
    $query = $this->db->get(); 

    if ($query->num_rows() > 0) { 
     return $query->result_array(); 
    } 
} 

每個位置的看法具有相同的foreach循環

<?php if ($modules) { ?> 
<?php foreach ($modules as $module) { ?> 
<?php echo $module;?> 
<?php } ?> 
<?php }?> 

主視圖

<div class="container"> 
    <div class="row"> 
    <?php echo $column_left; ?> 

    <?php if ($column_left && $column_right) { ?> 
    <?php $class = 'col-sm-6'; ?> 
    <?php } elseif ($column_left || $column_right) { ?> 
    <?php $class = 'col-sm-9'; ?> 
    <?php } else { ?> 
    <?php $class = 'col-sm-12'; ?> 
    <?php } ?> 

    <div id="content" class="<?php echo $class; ?>"> 
    <?php echo $content_top; ?> 
    <h1>Welcome to CodeIgniter!</h1> 

    <div id="body"> 
    <p>The page you are looking at is being generated dynamically by CodeIgniter.</p> 

    <p>If you would like to edit this page you'll find it located at:</p> 
    <code>application/views/welcome_message.php</code> 

    <p>The corresponding controller for this page is found at:</p> 
    <code>application/controllers/Welcome.php</code> 

    <p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p> 
    </div> 

    <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p> 

    <?php echo $content_bottom; ?> 
    </div> 
    <?php echo $column_right; ?></div> 
</div> 
+0

把'getModuleLayouts'方法的'foreach'塊放在查詢之外,用'where()'改變'or_where()'查詢生成器方法,而不是返回'$ query-> result_array()',將它設置爲新的新鍵數組將遵循foreach循環,該循環在循環外部返回數組。嘗試並檢查是否以這種方式工作。 – Tpojka

+0

@Tpojka你有個例子嗎? – user4419336

+0

那麼,如果你問這個問題,我沒有安裝你的應用程序,你的意思是? :)我認爲需要在那裏改變。你有沒有試過這個建議?那裏有什麼不同嗎? – Tpojka

回答

0

得到它的工作,我不得不使用foreach switch語句

<?php 

class Welcome extends CI_Controller { 

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

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

     foreach ($position as $key) { 

      switch ($key) { 
       case 'column_left': 

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

        $layout_module_results = $this->getlayoutsmodule($layout_id, 'column_left'); 

        if (!empty($layout_module_results)) { 
         foreach ($layout_module_results as $layout_module_result) { 
          $data['modules'][] = array(
           'position' => $layout_module_result['position'] 
          ); 
         } 
        } 

        $column_left = $this->load->view('column_left', $data, TRUE); 

        break; 

       case 'column_right': 

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

        $layout_module_results = $this->getlayoutsmodule($layout_id, 'column_right'); 

        if (!empty($layout_module_results)) { 
         foreach ($layout_module_results as $layout_module_result) { 
          $data['modules'][] = array(
           'position' => $layout_module_result['position'] 
          ); 
         } 
        } 

        $column_right = $this->load->view('column_right', $data, TRUE); 

        break; 

       case 'content_top': 

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

        $layout_module_results = $this->getlayoutsmodule($layout_id, 'content_top'); 

        if (!empty($layout_module_results)) { 
         foreach ($layout_module_results as $layout_module_result) { 
          $data['modules'][] = array(
           'position' => $layout_module_result['position'] 
          ); 
         } 
        } 

        $content_top = $this->load->view('content_top', $data, TRUE); 

        break; 

       case 'content_bottom': 

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

        $layout_module_results = $this->getlayoutsmodule($layout_id, 'content_bottom'); 

        if (!empty($layout_module_results)) { 
         foreach ($layout_module_results as $layout_module_result) { 
          $data['modules'][] = array(
           'position' => $layout_module_result['position'] 
          ); 
         } 
        } 

        $content_bottom = $this->load->view('content_bottom', $data, TRUE); 

        break; 
      } 

     } 

     $data['column_left'] = $column_left; 
     $data['column_right'] = $column_right; 
     $data['content_top'] = $content_top; 
     $data['content_bottom'] = $content_bottom; 

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