CodeIgniter Forums
Best Practice...where to generate a table - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Best Practice...where to generate a table (/thread-12289.html)



Best Practice...where to generate a table - El Forum - 10-13-2008

[eluser]blasto333[/eluser]
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?

Code:
function get_manage_table($customers)
{
    $this->table->set_template(get_sortable_table_template());
    $this->table->set_heading('<input type="checkbox" id="select_all" />',
    $this->lang->line('common_last_name'),
    $this->lang->line('common_first_name'),
    $this->lang->line('common_email'),
    $this->lang->line('common_phone_number'),
    ' ');
    
    foreach($customers as $customer)
    {
        $checkbox = "<input type='checkbox' id='customer_$customer->id' value='$customer->id'/>";
        $this->table->add_row($checkbox,$customer->first_name,$customer->last_name,
        $customer->email,$customer->phone_number,
        anchor("customer/edit/$customer->id", $this->lang->line('common_edit'),array('class'=>'thickbox')));
    }
    
    $output = $this->table->generate();
    $this->table->clear();
    
    return $output;
}



Best Practice...where to generate a table - El Forum - 10-13-2008

[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.


Best Practice...where to generate a table - El Forum - 10-13-2008

[eluser]blasto333[/eluser]
That makes sense, thanks


Best Practice...where to generate a table - El Forum - 10-13-2008

[eluser]blasto333[/eluser]
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:
http://muench.homeip.net/phppointofsale/index.php/login

username:admin
password:pointofsale

Click "Customers" at the top.


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


Best Practice...where to generate a table - El Forum - 10-13-2008

[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:
Code:
$CI =& get_instance();

// use $CI instead of $this - example:
$CI->lang->line('common_last_name'),



Best Practice...where to generate a table - El Forum - 10-13-2008

[eluser]blasto333[/eluser]
cool, thanks!