Welcome Guest, Not a member yet? Register   Sign In
Is my code OK any room for improvment
#1

(This post was last modified: 04-19-2017, 04:03 AM by wolfgang1983.)

Hello, On my function below I load modules in to set module positions

I just would like to know if the code could be improved if so how so? 

I am getting the expected result just not sure if could improve it.

[Image: 3JZiFYzj8Vdl.png]


PHP Code:
<?php

class Welcome extends CI_Controller {

    private 
$page;

    public function 
__construct() {
        
parent::__construct();
        
$this->page $this->router->class;

        
$this->load->model('design/layout_model');
    }

    public function 
index() {        
        
$modules $this->layout_model->getlayoutmodules($this->page);

        
$modules_content = array();

        
// Module Data Content
        
if ($modules) {
            foreach (
$modules as $module) {
                
$modules_name $module['modules_name'];

 
              // Load module library

                
$this->load->library('module/' $modules_name);

                
// Returns data only
                
$module_data $this->$modules_name->get();

                if (
$module_data) {

 
                   // Load the module view
                 
    $modules_content[$module['position']]['modules'][] = $this->load->view('module/' $modules_name$module_dataTRUE);
                }
 
            
}
        }

        
// Positions with modules
        
if ($modules_content) {
            foreach (
$modules_content as $key => $value) {    
                
$data[$key] = $this->load->view($key$modules_content[$key], TRUE);
            }
        }

        
// Positions with no modules
        
$layouts_positions_with_no_modules $this->layout_model->get_non_active_layouts_positions($this->page);

        foreach (
$layouts_positions_with_no_modules as $position) {
            
$data[$position] = '';
         }

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



Model


PHP Code:
<?php

class Layout_model extends CI_Model {

    public function 
getlayoutmodules($page) {
        
$this->db->select('l.*, m.*, lp.*');
        
$this->db->from('layouts l');
        
$this->db->join('modules m''m.modules_id = l.modules_id');
        
$this->db->join('layouts_positions lp''lp.layout_position_id = l.layout_position_id');
        
$this->db->where('l.page'$page);
        
$this->db->order_by('l.sort_order''asc');
        
$query $this->db->get();

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

        return 
FALSE;

    }

    public function 
get_active_layouts_positions($page NULL) {
        
$this->db->select('lp.layout_position_id');
     
   $this->db->from('layouts l');
     
   $this->db->join('layouts_positions lp''lp.layout_position_id = l.layout_position_id''LEFT');
     
   $this->db->where('l.page'$page);
     
   $query $this->db->get();
        
        if (
$query->num_rows() > 0) {
            return 
$query->result_array();
        }

        return 
FALSE;
    }

    public function 
get_non_active_layouts_positions($page NULL) {
        
$data = array();

        foreach (
$this->get_active_layouts_positions($page) as $position) {
         
   $this->db->where('layout_position_id !='$position['layout_position_id']);
        }

        
$query $this->db->get('layouts_positions');

 
       if ($query->num_rows() > 0) {

 
           foreach ($query->result_array() as $result) {
 
               $data[] = $result['position']; 
 
           }
        }

        return 
$data;
    }

    public function 
getlayoutpositions() {
        
$this->db->select('*');
        
$this->db->from('layouts_positions');
        
$query $this->db->get();

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

        return 
FALSE;
    }

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

Please don't post code questions in the Lounge forum ... you're a regular user here, should already know this.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB