Asked  2 Years ago    Answers:  5   Viewed   121 times

How would you structure the below page in Codeigniter?

I thought about creating seperate controllers for each section

  1. Left nav
  2. Content nav
  3. Login name
  4. Leaderboard

Excluding the content section (as this changes depending on the link on the left nav and content nav used as a kinda sub-menu). All the other sections remain roughly the same

I thought about doing:

Class User_Profile extends Controller

    function index()
        $this->load_controller('Leaderboard', 'Board');

        $this->Left_Nav->index(array('highlight_selected_page' => 'blah'));


        $content_data = $this->User->get_profile_details();

        $this->view->load('content', $content_data);



Obviously this load_controller does not exist but this functionaility would be useful. The controller for each section gets the data required from the model and then loads the page through $this->view->load()

It could be a headache to have this code in all the left nav links like News, Users, About Us, etc.. But then again not every nav link has all those sections so I need that flexability of having the sections as a "partial view"

Can anyone suggest a better way of doing this?



I can't vouch that this is the best approach, but I create a base controller like this:

class MY_Controller extends CI_Controller {

    public $title = '';
    // The template will use this to include default.css by default
    public $styles = array('default');

    function _output($content)
        // Load the base template with output content available as $content
        $data['content'] = &$content;
        $this->load->view('base', $data);


The view called 'base' is a template (a view that includes other views):

<?php echo doctype(); ?>
<html xmlns="">
        <?php $this->load->view('meta'); ?>
        <div id="wrapper">
            <?php $this->load->view('header'); ?>

            <div id="content">
                <?php echo $content; ?>

            <?php $this->load->view('footer'); ?>

What this achieves is that every controller wraps its output in the base template, and that views have valid HTML instead of opening tags in one view and closing in another. If I'd like a specific controller to use a different or no template, I could just override the magic _output() method.

An actual controller would look like this:

class Home extends MY_Controller {

    // Override the title
    public $title = 'Home';

    function __construct()
        // Append a stylesheet (home.css) to the defaults
        $this->styles[] = 'home';

    function index()
        // The output of this view will be wrapped in the base template

Then I could use its properties in my views like this (this is the 'meta' view that populates the <head> element):

echo "<title>{$this->title}</title>";
foreach ($this->styles as $url)
    echo link_tag("styles/$url.css");

I like my approach because it respects the DRY principle and the header, footer and other elements get included just once in the code.

Friday, August 19, 2022

Try Sparks' way:


Friday, November 4, 2022

I've never been actually taught, however I can let you know how I organise my CSS documents. As you say, I like to divide it up into "geographical" areas... i.e. the rules that apply to the header, the side bars, the main content, the footer, etc. And then, below these I add very specific rules, say if I need to style a form or a table on a particular page. Finally I add a "General Gubbins" section at the bottom when I add generic rules that may apply across the board.

Oh yes, I also like to add the designer's palette to the top for quick reference.

For example...

dark grey : #555555;
pink      : #d93575;

/* HEADER */
#header {...}
#header ul {...}

/* SIDE BAR */
#side {...}
#side ul {....}

#content p {....}

/* FOOTER */
#footer div {....}

/* FORMS */
form {...}
input {...}

.center {...}
strong {...}
floatleft {...}
Monday, August 22, 2022

I'm not sure if you call this "best" way, but a way, as described in the tutorial, is this,

in the database file, you have the default configuration, a part of which is:

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "user";
$db['default']['password'] = "database";
$db['default']['database'] = "db1";

now you can create another group, say we call it group1 and we want it to have everything the same as the default database settings except for the name, so you can do


then, when you want to use the second database, just go

$DB2 = $this->load->database('group1', TRUE); 

and then, instead of $this->db->foo() , you will do $DB2->foo()

alternatively (as suggested in comments by sbaaaang), you can do $this->db=$DB2; to keep everything the same

and you can extend this to multiple groups like this

 $DB1 = $this->load->database('group1', TRUE); 
 $DB2 = $this->load->database('group2', TRUE); 
 $DBn = $this->load->database('groupn', TRUE); 
Thursday, August 4, 2022

In Codeigniter you have a concept of a Controller and a Model, which each has their own separate folder.

They have setup their main router class such that it searches for corresponding controller and model files, it can even go recursive. This has nothing to do with any design pattern, it is just a folder organization.

I find this interesting because the framework makes an object of the User object and the programmer does not.

Yup, they have created a lot of stuff ready-made and to be used any time you want. The User class is used to control whole user system.

Basically, as you said, the main design pattern used is MVC, rest of the things are controlled by different core classes for a specific task.

Wednesday, December 14, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages