user572575
user572575

Reputation: 1049

How to call function from Controller to include into the View page with Codeigniter?

I want to call function from Controller to include into the View page with Codeigniter. Usually when I open any page, I call $this->load->view() in Controller for open that page. Now I want to include sub page into main page, but it can't include any function in View. I try to include function like this.

<body><? include(site_url().'/login'); ?></body>

<body><? include('/login'); ?></body>

<body><? include('./login'); ?></body>

I can open page with this link http://localhost/ci_house/index.php/login. but when I open main page for run my code it show these error.

A PHP Error was encountered

Severity: Warning

Message: include(http://localhost/ci_house/index.php/login) [function.include]: failed to open stream: no suitable wrapper could be found

Filename: views/main.php

Line Number: 8
A PHP Error was encountered

Severity: Warning

Message: include() [function.include]: Failed opening 'http://localhost/ci_house/index.php/login' for inclusion (include_path='.;C:\php5\pear')

Filename: views/main.php

Line Number: 8

I want to show 2 view in 1 page .

function test1()
{ $data['var_for_login_view'] = 'get table1';
  $this->load->view('main1',$data);
}
function test2()
{ $data['var_for_login_view'] = 'get table2';
  $this->load->view('main2',$data);
}

In views/main.php:

$this->load->view('test1');
$this->load->view('test2');` 

I want to show like

<body>
include('main1.php');
include('main2.php');
</body>

but I can show like this in Codeigniter.

Upvotes: 0

Views: 2757

Answers (3)

Damien Pirsy
Damien Pirsy

Reputation: 25445

I really can't understand your question well, but hey, you can "include" any view within another view without problems..

In main.php:

$this->load->view('login');

You don't even need to pass it the paramwters, as they are buffered so available to any child view you might insert. But please, be more clear on what you actually need.

If you want to include in main() the same views you load in login() method, of course you don't have to include a CI URI, but just create the variables you need to pass inside the controller's method login(), and then call whatever view you want, be it a view which is designed for this specific method or for any other controller's method.

So, for.ex.

function login()
{
  $data['var_for_login_view'] = 'a variable';
  $data['var_for_this_view'] = 'another variable';
  $this->load->view('main');
}

In views/main.php:

echo $var_for_this_view;
$this->load->view('login');
echo $var_for_login_view;

// see? $data was not passed to $this->load->view('login'), but it's still there nonetheless!

Upvotes: 1

Idham Perdameian
Idham Perdameian

Reputation: 2277

You can't include using site url, use this $this->load->view('template', $data); for codeigniter.

Upvotes: 0

Vikk
Vikk

Reputation: 3363

What I understand is this: You have some functions defined in one of your controllers and you want to be able to call these functions from another controller/view.

If this is correct, then we can't normally do this. Here are a couple of alternatives:

  1. Move these functions to a library/helper. Load that library/helper wherever you want and then you can call these functions.

  2. If you absolutely need to call these functions from the controller, you can look into HMVC extension.

Upvotes: 0

Related Questions