Welcome Guest, Not a member yet? Register   Sign In
Best Practice...where to generate a table

In a program I am writing, I have a module for adding, updating, deleting, and viewing customers. In my customer model, I created a function get_manage_table, that creates an html table and returns as a string.

The result of this function is then passed into a view from my controller and outputted. Is this the way to go?

function get_manage_table($customers)
    $this->table->set_heading('<input type="checkbox" id="select_all" />',
    ' ');
    foreach($customers as $customer)
        $checkbox = "<input type='checkbox' id='customer_$customer->id' value='$customer->id'/>";
        anchor("customer/edit/$customer->id", $this->lang->line('common_edit'),array('class'=>'thickbox')));
    $output = $this->table->generate();
    return $output;

[eluser]Colin Williams[/eluser]
Your model is generating HTML, which is never a good thing in my book. I would relegate this to a helper with a function name like customer_table_form() if it must be abstracted.

That makes sense, thanks

When I started to go do this I realized that I cannot put this function in a helper because it needs access to the language and table libraries.

I was thinking of moving the logic to the view, but this function is also used for AJAX. A user can do a search and the table is sent back and updated using the dom (really easy with jquery).

So the only place I could move it to is the controller.

If you want to see this in action go to:


Click "Customers" at the top.

I am trying to follow best practices and this did not feel like a good practice.

[eluser]Pascal Kriete[/eluser]
You can still put it in a helper (wohoo). You just need to grab an instance of the current CI super object:
$CI =& get_instance();

// use $CI instead of $this - example:

cool, thanks!

Theme © iAndrew 2016 - Forum software by © MyBB