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

[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;
}
#2

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

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

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

[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'),
#6

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




Theme © iAndrew 2016 - Forum software by © MyBB