Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 35 revisions

Category:Libraries Category:Helper Category:Form Category:Table

How about not having to create any tables around forms anymore?

How to use it:

This will create a row with a single iten and the key will be the heading: [code] $form['Title'] = 'Title input filed goes here'; [/code] This will create a row with two itens and the second keys will be the headings: Note the [1]. Numeric keys will be multi item rows. You can add as many items to a row as you need.

[code] $form[1]['slug'] = '<input type="text" name="slug" />'; $form[1]['Group'] = '<input type="text" name="group" />'; [/code] 3 items: [code] $form[2]['slug'] = '<input type="text" name="slug" />'; $form[2]['Group'] = '<input type="text" name="group" />'; $form[2]['Template'] = '<input type="text" name="Template" />'; [/code]

This will create the form and the table: Note the two 'not intended' lines, that is all it takes to wrap the table. [code] function addPage() { $data = form_open('admin/addPage', $attributes); $form['Title'] = '<input type="text" name="Title" value="" maxlength="200" size="50" />'; $form[1]['slug'] = '<input type="text" name="slug" />'; $form[1]['Group'] = form_dropdown('groups', user_groups(), 'User'); $form['Body'] = '<textarea name="Body" rows="20" cols="80" /></textarea>'; $form[''] = '<input type="submit" name="submit_page" value="Submit" />'; $this->load->library('table'); $data .= $this->table->form_table($form); $data .= '</form>'; $this->load->view('layout/blank', $data); } [/code]

Form to Table Just save this as /system/application/libraries/MY_Table.php [code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Table extends CI_Table {

function __construct()
{
    parent::CI_Table();
}

function form_table($form)
{
    $return = '<table style="width: auto;"><tr><td>';
    $this->set_template($this->form_table_tmpl());
    foreach($form as $k=>$v){
        if(is_numeric($k)){
            $head = array_keys($v);
            $this->set_heading($head);
            $return .= $this->generate($v);
        }else{
            $this->set_heading($k);
            $this->add_row($v);
            $return .= $this->generate();
        }
        
        $this->clear();
    }
    $return .= '</td></tr></table>';
    return $return;
}


function form_table_tmpl()
{
    return array (
        'table_open'          => '<table class="form">',
        
        'heading_row_start'   => '<tr class="form">',
        'heading_row_end'     => '</tr>',
        'heading_cell_start'  => '<th class="form">',
        'heading_cell_end'    => '</th>',
        
        'row_start'           => '<tr class="form">',
        'row_end'             => '</tr>',
        'cell_start'          => '<td class="form">',
        'cell_end'            => '</td>',
        
        'row_alt_start'       => '<tr class="form">',
        'row_alt_end'         => '</tr>',
        'cell_alt_start'      => '<td class="form">',
        'cell_alt_end'        => '</td>',
        
        'table_close'         => '</table>'
        );
}

}

?> [/code]

Add this to your css file [code] table.form { width: 100%; } [/code]

If you need help or even better have improvements let me know, please. I watch this thread: [url=http://codeigniter.com/forums/viewthread/48152/]Forum[/url]

Clone this wiki locally