Welcome Guest, Not a member yet? Register   Sign In
How do use includes?
#1

[eluser]Nathan Payne[/eluser]
Hey,

I am new to code igniter and want to know how I use includes.

I have started building a web page but don't want to replicate every line of code so includes are the best answer, but how do I use them with code igniter?

Thanks
#2

[eluser]bastones[/eluser]
You can call functions from within your controller like $this->functionName(); such as:

Code:
<?php
    class Blog extends Controller {
        public function index() {
            echo 'hello world!';
        }
        public function register() {
            $this->index(); // output: hello world
        }
    }
?>
#3

[eluser]Nathan Payne[/eluser]
I meant in terms of headers and footers. Thanks though.
#4

[eluser]bastones[/eluser]
Ah. If you want specific functions to point to specific views you use something like this:

Code:
<?php
$array=array('var'=>'Headers','var1'=>'Footers');
$this->load->view('view_file_name',$array) // dont include the .php extension
?>

Remember the array elements become variables, so to get the array contents you use: echo $var;
#5

[eluser]Aken[/eluser]
Yeah, just create views for your header and footer files, then call them in your controllers.
#6

[eluser]Steve Grant[/eluser]
The way I do it is to have all output directed to a single view file, which includes a number of generic "component" files. In the controller, I then pass an array of files which contain the *actual* page content (i.e. stuff that is specific to the particular page you're viewing) and one of the "component" files contains a foreach loop on that file list to recursively include the files in the list.

File Structure:
Code:
views/
- index.php (single view file)
- global/
--- header.php (globally-included page header)
--- footer.php (globally-included page footer)
--- content.php (globally-included page "content")
- content/
--- controller1/
----- controller1_action1_init.php
----- controller1_action1_error.php
----- controller1_action1_done.php
--- controller2/
----- controller2_action1_init.php
----- controller2_action2_init.php

Controller (extract):
Code:
function index()
{
  $this->data['file_list'][] = $view_path . '/content/controller1/controller1_action1_init.php';
  // if we want to add more content, we can do... this allows us to separate
  // the page into manageable blocks
  $this->data['file_list'][] = $view_path . '/content/controller1/controller1_action1_done.php';

  // load the view
  $this->load->view('index', $this->data);
}

Single view file:
Code:
<html>
<?php include_once('global/header.php'); ?>

<?php include_once('global/content.php'); ?>

<?php include_once('global/footer.php'); ?>
</html>

Content file:
Code:
<?php
  foreach($file_list as $file)
  {
    include_once($file);
  }
?>




Theme © iAndrew 2016 - Forum software by © MyBB