Welcome Guest, Not a member yet? Register   Sign In
Creating an HTML table using database results
#1

[eluser]carlhussey[/eluser]
Hey Guys,

I'm having a little trouble with this mostly because I'm still learning. Anyway, Im trying to create a table and then have it add a new row to the table for each result. Here is my code..

Code:
$this->load->library('table');
        
        $tmpl = array (
                    'table_open'          => '<table>',

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

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



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


                $this->table->set_template($tmpl);
                
                $this->table->set_heading('#', 'Cracker', 'Reason', 'Date Added', 'Manager', 'Status');

                    $query = $this->db->query("SELECT id, cracker, reason, added, manager FROM crackers");

             foreach ($query->result_array() as $row)
                {
  
                $row = $query->row();
                $cracker = $row['cracker'];

                    $this->table->add_row('1', '', '', '', '', '');
    
                }
    
            $data['table'] = $this->table->generate();    
        
        $this->load->view('crackers', $data);
        $this->load->view('footer', $data);

Basicly, where im stuck is.. $this->table->add_row('1', '', '', '', '', '');

I'm not sure if thats how you would add a new row to the table? I figured it would need to be in a loop and every time it gets a result it runs the add row.

Am I even close?
#2

[eluser]Aken[/eluser]
You can load data into the table straight from the DB query object.
Code:
$this->load->library('table');
        
$tmpl = array (
    'table_open'          => '<table>',

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

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

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

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

$this->table->set_heading('#', 'Cracker', 'Reason', 'Date Added', 'Manager', 'Status');

$query = $this->db->query("SELECT id, cracker, reason, added, manager FROM crackers");

$data['table'] = $this->table->generate($query);    

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

And in the future, please use the Reactor forum to discuss your application's code. This section is meant for sharing completed and working code with others.
#3

[eluser]carlhussey[/eluser]
I get that and I had it like that before and it worked fine. I just couldnt figure out how to customize it with my own stuff if i didnt want a database result there. For example if i wanted an incrementing number in the first column or links in the last one? Rather than only show content just stored in the field, id like to be able to tweak certain data in certain columns.

Sorry about the wrong forum, didnt see the other one.
#4

[eluser]Aken[/eluser]
Then use add_row() like you were trying (though not going through results properly):
Code:
$this->load->library('table');
        
$tmpl = array (
    'table_open'          => '<table>',

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

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

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

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

$this->table->set_heading('#', 'Cracker', 'Reason', 'Date Added', 'Manager', 'Status');

$query = $this->db->query("SELECT id, cracker, reason, added, manager FROM crackers");

foreach ($query->result() as $row)
{
    $this->table->add_row($row->id, $row->cracker, $row->reason, $row->added, $row->manager, '');
}

$data['table'] = $this->table->generate();    

$this->load->view('crackers', $data);
$this->load->view('footer', $data);
#5

[eluser]carlhussey[/eluser]
Awesome, thanks so much for the help! Just what I needed Smile




Theme © iAndrew 2016 - Forum software by © MyBB