CodeIgniter Forums
Stupid question about include() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Stupid question about include() (/showthread.php?tid=2818)



Stupid question about include() - El Forum - 08-26-2007

[eluser]Phoenixheart[/eluser]
(Sorry, but I am new to CI)
Assuming I have a controller at projects.php, in which I have GetProjects() function. Also I have a view called projects_view.php.
I wanted to call GetProjects() along with the view. So in projects_view.php I put this code:
Code:
// my HTML here
<?php
    include('index.php/projects/GetProjects');
?>
// my HTML here
But I received this error:
Code:
Message: include(index.php/projects/GetProjects) [function.include]: failed to open stream: No such file or directory
Does it mean we can't use php's include() with CI's URI? Or am I doing something wrong?
Thanks in advance!


Stupid question about include() - El Forum - 08-26-2007

[eluser]snaggy[/eluser]
I can't answer your question since I don't know exactly what you're trying to do, but if you just want to include the output of a controller's function you should pass it from the controller

$this->load->view('view name', $variable)

you can pass whatever you want through that variable, even what you get from another function/view. You shouldn't need to include anything in the view file.


Stupid question about include() - El Forum - 08-26-2007

[eluser]Michael Wales[/eluser]
Not sure why you are including either, but I know what the problem is. index.php/projects/GetProjects is not a valid path (I mean, unless you really do have a folder called 'index.php', with a sub-folder of 'projects', which house a file named 'GetProjects' - note the lack of extensions).

include() uses literal paths on your filesystem to include a file. So, to include a file that is in your controllers folder, from a file in your views folder, you would use:
Code:
include('../controllers/GetProjects.php');

But once again - there is no reason for you to do this. What exactly are you trying to do?


Stupid question about include() - El Forum - 08-26-2007

[eluser]coolfactor[/eluser]
CodeIgniter uses a front-controller approach. The only thing that should trigger a controller file is your front-controller, which is typically an index.php file, but it can be named anything you want. The front controller is always the very first file loaded by Apache (hence "front") when requesting a page on your site. You can have multiple front controllers in a site, but their own difference would be which application they invoke. They don't do anything else.


Stupid question about include() - El Forum - 08-28-2007

[eluser]Phoenixheart[/eluser]
[quote author="snaggy" date="1188163807"]I can't answer your question since I don't know exactly what you're trying to do, but if you just want to include the output of a controller's function you should pass it from the controller

$this->load->view('view name', $variable)

you can pass whatever you want through that variable, even what you get from another function/view. You shouldn't need to include anything in the view file.[/quote]

Thanks to your help I have eliminated that error. But now, does $this->load->view('view name', $variable) return any value? Since I want to load a child view into a parent view - my intention is using Ajax features.

Code:
function index()
{
    $this->load->model('getprojects_model', '', true);
    $data = array(
        'initial_projects' => $this->getprojects_model->GetProjects(1)
    );
    $this->load->view('projects_view', $data);
}

function GetProjects($page = 1)
{
    $view = $this->load->view('getprojects_view', $this->getprojects_model->GetProjects($page));
}
Here, I want 'initial_projects' to hold the child view, and I'll output it on the main view. But it does not contain anything...
Please help me with... Thanks in advance.


Stupid question about include() - El Forum - 08-28-2007

[eluser]Michael Wales[/eluser]
You really, really, ought to read over the User Guide.

Controller:
Code:
function index() {
  $data['initial_projects'] = $this->load->view('getprojects_view', , TRUE);
  $this->load->view('projects_view', $data);
}



Stupid question about include() - El Forum - 08-28-2007

[eluser]Phoenixheart[/eluser]
Oops! (Cough) Sorry... (What a shame).
Yes I've read it, but... how can I miss it?
Thank you thank you!


Stupid question about include() - El Forum - 08-28-2007

[eluser]Michael Wales[/eluser]
I'm really not trying to be mean here at all, but you are struggling with the basic underlying fundamentals of the framework.

We don't mind helping here at all - the more the merrier - but I highly recommend you start reading the User Guide at CodeIgniter at a Glance and continue until you finish Helpers.

That's your core knowledge that you should know before you even begin editing config.php. Now, you can use the rest of the manual as reference.