Welcome Guest, Not a member yet? Register   Sign In
[Beginner] output order of views/controller code on page and HTML Table Class tag attribute support
#1

[eluser]Unknown[/eluser]
Hello, I've only been using Codeigniter for 24 hours (apologies for stupid questions etc.)

The controller code below displays a simple table of contacts from a MySQL database. I have standard header and footer views I'm using, as I'm doing everything with the table and database classes, I thought it made sense to keep the DB query and table writing code in the controller rather than in the view - however the trouble with the current output is the table is output *before* the header and footer.

(a) Is my methodology wrong - should *any* HTML output always be written in the view, even if that makes I end up with a large block of ugly PHP code amongst my HTML tags?
(b) is there some way to flush the output buffer after loading views etc. in exceptional circumstances?

Code:
function index() {

        $data['title'] = "Group Contacts";

        $this->load->view('header.php', $data);

        $this->load->library('table');

        $query = $this->db->get('local_group_contacts');

        if ($query->num_rows() > 0) {
    
                $this->table->set_heading('Area', 'Contact', 'Telephone');


                foreach ($query->result() as $row) {
                    $this->table->add_row($row->area, $row->fullname, $row->tel);
                }

                echo $this->table->generate();


        }

        $this->load->view('footer.php', $data);

    }

Secondly, the HTML Table class seems a bit limited - apparently no support for 'scope' attributes, opening/closing <thead>,<tfoot>,<tbody>, and adding class tags or other attributes to specific cells. Has someone somewhere written a better one?

Thanks,
William
#2

[eluser]InsiteFX[/eluser]
I would create a MY_Controller and add the table template to
it, this way it will always be avaiable to you in your views.

Change your controller to this.
Code:
function index() {

$tmpl = array (
    'table_open'          => '<table border="0" cellpadding="4" cellspacing="0">',

    'heading_row_start'   => '<tr>',
    'heading_row_end'     => '</tr>',
    'heading_cell_start'  => '<th>',
    'heading_cell_end'    => '</th>',

    'row_start'           => '<tr>',
    'row_end'             => '</tr>',
    'cell_start'          => '<td>',
    'cell_end'            => '</td>',

    'row_alt_start'       => '<tr class="alt">',
    'row_alt_end'         => '</tr>',
    'cell_alt_start'      => '<td>',
    'cell_alt_end'        => '</td>',

    'table_close'         => '</table>'
);

$this->table->set_template($tmpl);

$data['title'] = "Group Contacts";

$this->load->view('header.php', $data);

$this->load->library('table');

$query = $this->db->get('local_group_contacts');

if ($query->num_rows() > 0) {

$this->table->set_heading('Area', 'Contact', 'Telephone');


foreach ($query->result() as $row) {
$this->table->add_row($row->area, $row->fullname, $row->tel);
}


}

$this->load->view('footer.php', $data);

}

Add this to your view
Code:
&lt;?php echo $this->table->generate(); ?&gt;

InsiteFX
#3

[eluser]Federico BaƱa[/eluser]
I'd put the database query into a model (no matter how simple it is) first.
Then i'd create a third view for the table, and then load header, table_view and footer on the controller.

Here's a short snippet
Code:
// Model
class Contacts_model extends Model {

    ...

    function get_contacts()
    {
        $query = $this->db->get('local_group_contacts');

        if ($query->num_rows() > 0) {
              return $query->result();
        }
        
        return array();
    }

}

Code:
// Controller
function index()
{
     ... load model
     ... load table library
     $data = array('table' => $this->table->generate());
     ... load header
     $this->load->view('table_view', $data);
     ... load header
}

Code:
// Table View
&lt;?php echo $table; ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB