3

So I decided to give codeigniter a chance and convert my existing site which is all custom php to the codeigniter framework. But trying to figure out how to do this best in codeigniter that I was doing from my original site. In the original site a person would visit a page such as index.php. In the index.php code its broken into sections like so (This is just a very simple example):

index.php:

<html>
<head></head>
<body>

<div id="top">{$SECTION1}</div>
<div id="main">{$SECTION2}</div>
<div id="bottom">{$SECTION3}</div>

</body>
</html>

Then in my main php file that is included in every single page I run a query that grabs all "modules" that are assigned to each section above for the index.php page. So give me all modules that are assigned to {$SECTION1}, {$SECTION2} and {$SECTION3} for index.php. A module in my original system is simply a php file that does a specific job so I might have a module called latest_story.php and in the script it gets the last 10 stories from the database and display the results.

latest_story.php

include('class/story/story_class.php');

$story = new Story($databaseconn);
$latest_story = $story->findLatestStory(10);

//If results back from $latest_story loop through it
//and display it however I want

Then using output buffering I execute each module then take the outputted information from each module assigned to each section and replace that section with the outputted data.

Can this be done in codeigniter? If so can someone point me in the right direction in doing this in codeigniter?

EDIT

To give a more clear view of what Im doing in the original system I run a query on every single page to determine what "modules" to grab for the current page the person is viewing then run this code

    foreach($page_modules AS $curr_module)
    {

        $module_section = intval($curr_module->section);
        $module_file = $global_function->cleanData($curr_module->modfile);
        $module_path = MODS .$module_file;

        if(is_file($module_path))
        {

            ob_start();
            include($module_path);
            ${'global_pagemod' .$module_section} .= ob_get_contents();
            ob_end_clean();

        }

    }

Then I simply take each variable created for each section and replace {$SECTION[n]} with that variable in the template.

ADDITIONAL EDIT

What Im trying to do is be able to create "modules" that do specific tasks and then in the backend be able to dynamically place these modules in different sections throughout the site. I guess you can say its like wordpress, but a more simplier approach. Wordpress you can add a module and then assign it to your pages either in the left column, middle or right column. Im trying to do pretty much the same thing.

3 Answers 3

2

Basically, Codeigniter is an MVC framework. To use it, you really want to embrace that rather than include files. You can rejig your code as:

In your controller:

function example()
{

  $this->load->model('story_model','story');
  $data['section1'] = $this->story->latest();
  $data['section2'] = $this->story->top_stories(5);
  $data['section3'] = $this->story->most_popular();
  $this->load->view('example', $data );
}

In your model:

function latest()
{
  return $this->db->limit(1)->get('stories');
}
etc.

In your view:

<html>
....
<div id="latest">
<h2><?php echo $section1->title; ?></h2>
<?php echo $section1->body; ?>
</div>
<div id="top">
<?php foreach $section2 as $popular { ?>
<h2><?php echo $popular->title; ?></h2>
<?php echo $popular->body; ?>
<?php foreach} ?>
</div>
</html>

If you wanted a second page, with different content but the same layout, you'd add this to your controller:

function second_page()
{

  $this->load->model('tags_model','tag');
  $this->load->model('authors_model','author');
  $data['section1'] = $this->tag->latest();
  $data['section2'] = $this->tag->top_tags(5);
  $data['section3'] = $this->author->most_popular();
  $this->load->view('example', $data );
}

and add in new models etc.

Sign up to request clarification or add additional context in comments.

6 Comments

@Apemantus would this work if I have multiple modules assigned to one section? That is how they are loaded, by section then by the view order. So for index.php in section2 I want the latest 10 stories module to show first then under that I want a different module to load. All dynamically assigned via the database. So one section could hold 10-20 different modules on one page.
@Apemantus In your example above your manually telling the controller what information to pass to the view but I wouldnt know what modules to load per controller/page until I query the db to find out what modules were assigned to this page for each section.
Sure. I'm not 100% on what the structure of your site would be, but you probably want to looking at things like template libraries and partials and so on. Basically, in your controller you probably want to be getting the data for each "module" from your models and passing them to a view which returns the data to the controller, and then passing all those rendered views to your layout. It's arguably more precise for you to use HMVC/MVC triads: bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/… but if you're just starting with Codeigniter, that may be OTT for now.
Umm. Maybe. That sounds a bit like the house that Jack built... Firstly: controllers aren't autoloaded: they're loaded (effectively) by the request made by the user. Controllers can have access to models (and helpers and libararies) by default/autoload. It sounds like you're trying to create one monolithic controller with one method/action that takes in everything dynamically. That sounds like a really inflexible, non-scalable way of doing things that requires a lot of hacking. I'd say take a step back and look at having data in databases/models, html in views, access in controllers etc.
(Running up against SO's comment limit, but specifically in terms of "autoloaded controllers", assuming you mean: action/infomation available to many methods of a controllers or different controllers themselves, the options are: extend the base controller (i.e. MY_Controller); add code into __construct (for MY_Controller or just a normal controller); put it in a (autoloaded) library and call it from there.)
|
2

You can pass the retrieved data as an array to the main view and then access that data on the subviews. For example: Main View

$posts = $this->post_model->fetch_all();
$top_data = $this->more_data_model->fetch_more_data();
$data = array('posts'=>$posts,'top_data'=>$top_data);
$this->load->view('main_view',$data);

Then on the main view you load the subviews:

$data = array('data_name'=>$top_data);
$this->load->view('top_view',$data);

And then on the subview you just use the data however you want. There might be a more effective way to do this, but i believe this'll do the trick. :)

3 Comments

I dont know if that will work. Basicaly all Im trying to do is dynamically include php files into the view per section. Each page could have different php files to include, for example index.php might have the latest_story.php file to include for {$SECTION1} but on main_page.php I want to load all_stories.php in {$SECTION1} as well as sub_stories.php under all_stories.php in {$SECTION1}
I updated my original post. Please read and let me know if that helps better explain what Im trying to do.
@John You can always use a bigass array with the values assigned, and the just call for instance <?= $modules['top']; ?> which is assigned on the array. That's the only thing that crosses my mind after reading the edit.
2

I do it like this:

Controller:

function about()
{

    $user_session = $this->model_user_lite->Session_Check();
    $data['auth'] = $user_session;
    $user_id = $this->model_user_lite->Session_UserID();
    $data['user_id'] = $user_id;


    $this->load->view( 'main_about.php', $data );

}

View:

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

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

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




    <div class="box">
        <div class="box-top"></div>
        <div class="box-center">
            <div class="content">



                    <center>
                        <h2><?= lang('MAIN_ABOUT_CAPTION_TEAM'); ?></h2>
                        <div class="hr"></div>
...

Header Base:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="shortcut icon" href="<?= base_url(); ?>/favicon.ico" type="image/x-icon">
<link rel="icon" href="<?= base_url(); ?>/favicon_animated.ico" type="image/x-icon">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />



    <meta property="fb:admin_id" content="677920913" />
    <meta property="fb:app_id" content="<?= FACEBOOK_APPID; ?>" />
    <meta property="og:site_name" content="<?= SITE_NAME; ?>" />
<?php if ( empty( $og_title ) === FALSE ): ?>
    <meta property="og:title" content="<?= $og_title; ?>" />
<?php endif ?>
...

This way you have a clean file system, seperate header, you can even have more headers just as you like, and can assign all the data from the controller to the view.

**Variables that are assigned from the controller, also go to views loaded by your "main view".

I'm using it in a major dating website and it works like a charm.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.