Welcome Guest, Not a member yet? Register   Sign In
Loading controllers from a view
#1

[eluser]heaversm[/eluser]
Hi - sorry - I'm traditionally a Flash programmer and very new to the code igniter way of thinking. In flash, you build a class with a bunch of functions, and if you want to call a function, you just put that function name into your code wherever you want it executed. Does that not work in CodeIgniter? I'm trying not to load a ton of code into my index function, so I want to put it into multiple functions, but I want all those functions to load when the user accesses my URL. For example, say I have a controller called Portfolio. I have two functions:

Code:
function index(){
    $data['main_content'] = 'portfolio_home';
    $this->load->view('portfolio',$data
}

function listProjects(){
    $this->load->library('table');
    $query = $this->db->query("
        SELECT clients.client, project_list.project, project_list.role, project_list.url
        FROM clients, project_list
        WHERE clients.id = project_list.client_id
        GROUP BY clients.client, project_list.project
        ");
        
    echo $this->table->generate($query);
}

How can I specify in my view somewhere to execute listProjects()? If I put this into the index function, I can't specify where the table is generated. Plus, as development goes on, I get a really long index function. But if I keep it as a separate function, I have to load a different view entirely in order to see the result. Sorry, I know it's a remedial question, but nothing I've read has been able to explain this in a way that makes sense to me yet. Thanks in advance,

Mike
#2

[eluser]LuckyFella73[/eluser]
Hi Mike,

you could do something like:
Code:
<?php
function index()
{
    $data['main_content'] = 'portfolio_home';
    $data['db_data_table'] = $this->_listProjects();
    
    $this->load->view('portfolio',$data);
}

function _listProjects(){
    $this->load->library('table');
    $query = $this->db->query("
        SELECT clients.client, project_list.project, project_list.role, project_list.url
        FROM clients, project_list
        WHERE clients.id = project_list.client_id
        GROUP BY clients.client, project_list.project
        ");
        
    return $this->table->generate($query);
}  
?>

and then echo $db_data_table in your view. I never worked with the table
library but that could be the way to go. If you need the same function
in other controllers too you better set up a library or helper.
#3

[eluser]Bart Mebane[/eluser]
One way you can do it is to generate the table in the controller and pass it to your view:
Code:
function index(){
    $data['main_content'] = 'portfolio_home';
    $data['project_list'] = $this->listProjects();
    $this->load->view('portfolio',$data);
}

function listProjects(){
    $this->load->library('table');
    $query = $this->db->query("
        SELECT clients.client, project_list.project, project_list.role, project_list.url
        FROM clients, project_list
        WHERE clients.id = project_list.client_id
        GROUP BY clients.client, project_list.project
        ");
        
    return $this->table->generate($query);
}

Then in the view:
Code:
echo $project_list;
#4

[eluser]pickupman[/eluser]
This is a good question actually. Part of the hard part is the learning curving coming over to PHP/CI and having to use object oriented programming.

Here's how I would use your code
Code:
function index(){
    $data['main_content'] = 'portfolio_home';

    $data['listProjects'] = $this->_listProjects(); //Run _listProjects method and save table to variable
    $this->load->view('portfolio',$data);
}

//This method/function could be put into a library/model as it is accessing DB
//Note the _ underscore this prevents the url being directly accessible
function _listProjects(){
    $this->load->library('table');
    
    //Use active record syntax for safer queries
    $this->db->select('clients.client, project_list.project, project_list.role, project_list.url');
    $this->db->from('clients, project_list');
    $this->db->where('clients.id', 'project_list.client_id');
    $this->db->group_by('clients.client, project_list.project');
    $query = $this->db->get();        
        
    return $this->table->generate($query);
}

When I first starting using CI, I had some big index() functions. You will find your code will be easier to manage if you can segment some of this code into libraries or other private methods in the controller.
#5

[eluser]heaversm[/eluser]
Wow, this clears so much up for me. Didn't know how to execute one function from another function, or how to pass that data on to the view. Thanks, and good call on using the private functions / active record syntax. This will make my life much easier.
#6

[eluser]pickupman[/eluser]
Glad you picked up the concept easily. Once you get into it more, you will probably find that listProjects would be nice to have in a library. That way you can load/use it from any controller/view.




Theme © iAndrew 2016 - Forum software by © MyBB