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

Category:Helper Category:Form Category:Table

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

How to use it: [code] This will create a row with a single iten and the key will be the heading: $form['Title'] = 'Title input filed goes here'; [/code] This will create a row with two itens and the second keys will be the headings: [code] $form[1]['slug'] = '<input type="text" name="slug" />'; $form[1]['Group'] = '<input type="text" name="group" />'; [/code] This will create the form and 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" />'; $data .= form_table($form); $data .= '</form>'; $this->load->view('layout/blank', $data); } [/code]

Form to Table Just save this in a helper file: [code] function form_table($form) { $return = '

'; $this->load->library('table'); $this->table->set_template(table_form()); foreach($form as $k=>$v){ if(is_numeric($k)){ $head = array_keys($v); $this->table->set_heading($head); $return .= $this->table->generate($v); }else{ $this->table->set_heading($k); $this->table->add_row($v); $return .= $this->table->generate(); }
        $this->table->clear();
    }
    $return .= '</td></tr></table>';
    return $return;
}

[/code]

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

This is the template, which only adds the class="form" value: [code] function table_form() { return array ( 'table_open' => '

',
    '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]

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