Welcome Guest, Not a member yet? Register   Sign In
MVC Question
#1

[eluser]Unknown[/eluser]
I have decided that it is time to learn a framework and decided that CI will be the one I use. I am familiar with the principles of MVC, but have never coded a project with it.

My question is about the html table helper and where the various methods should be executed. I would like to follow MVC conventions as strictly as possible as a learning exercise. Here are the ways I have thought of doing it.

1. Retrieve the data with my model, have the model modify and generate the table, and return the table to the controller. The controller would set the table into a variable to be injected into a view.

2. Retrieve the data with my model and return the query result object or array to the controller. Table would be built and generated by the controller and set to a variable for injection into a view.

3. Retrieve data with model, return result object or array to controller, build the table in the controller and set to variable for injection in view. Execute the actual $this->table->generate() statement in the view.

4. Retrieve the data with model, return array to controller, manipulate data in the controller, inject array into view. The view would loop through the array and manually build the table.

I'm leaning toward option 4 because it allows for the view to control the styling and structure of the table. Is this the best MVC way of doing this?
#2

[eluser]Otemu[/eluser]
Hi,

This can depend but commonly it goes like this:

Controller loaded
//get the model data - store in an array
Model
//retrieves the data, returns this to controller
Controller
//has model data, sends to view as an array
View
//Loops through data and displays

Example code taken from codeigniter guide:

Code:
class Blog_controller extends CI_Controller {

    function blog()
    {
        $this->load->model('Blog');

        $data['query'] = $this->Blog->get_last_ten_entries();

        $this->load->view('blog', $data);
    }
}

Alternatively you could have a controller function that builds the table if you have complex logic that can't be handled by the view something like this:

Code:
public function index()
{
$myTable= $this->getmyTable(); //get data from model
$createMyTable = $this->_renderTable($myTable); /
$this->load->view('testnavigation.php', $createMyTable);
}
function _renderTable($tree){  
//your code here
}

A note about helper files in Codeigniter
Quote:Unlike most other systems in CodeIgniter, Helpers are not written in an Object Oriented format. They are simple, procedural functions. Each helper function performs one specific task, with no dependence on other functions.







Theme © iAndrew 2016 - Forum software by © MyBB