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

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

How to use it:

This will create a row with a single item and the key will be the heading: [code] $form['Group'] = '<input type="text" name="group" />'; [/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" style="width: 100%;">',
        
        'heading_row_start'   => '<tr class="form">',
        'heading_row_end'     => '</tr>',
        'heading_cell_start'  => '<th class="form" style="text-align: left;">',
        'heading_cell_end'    => '</th>',
        
        'row_start'           => '<tr class="form">',
        'row_end'             => '</tr>',
        'cell_start'          => '<td class="form style="text-align: left;"">',
        'cell_end'            => '</td>',
        
        'row_alt_start'       => '<tr class="form">',
        'row_alt_end'         => '</tr>',
        'cell_alt_start'      => '<td class="form style="text-align: left;"">',
        'cell_alt_end'        => '</td>',
        
        'table_close'         => '</table>'
        );
}

}

?> [/code]

Here is one more sample with hidden fields and radio buttons: [code] function Form() { $action = $this->uri->segment(3); $checkYes = $checkNo = FALSE; $this->dbdata = $this->set_form(); extract($this->dbdata); if($Showroom == 1 || $Showroom == 'Yes' ){ $checkYes = TRUE; } if($Showroom == 0 || $Showroom == 'No' ){ $checkNo = TRUE; }

   $attributes = array('name' => 'vehicle');
   $this->tpl['body'] =  form_open("billboard/bb_vehicle/$action", $attributes);
   $data = array(
          'name'        => 'Title',
          'id'          => 'Title',
          'value'       => $Title,
          'maxlength'   => '30',
          'size'        => '10',
          'style'       => '',
        );
   $form['Title'] = form_input($data);
   $form[1]['Showroom'] = form_radio('Showroom', 'Yes', $checkYes) . 'Yes';
   $form[1][''] = form_radio('Showroom', 'No', $checkNo) . 'No';
   $this->load->library('table');
   $this->tpl['body'] .= $this->table->form_table($form);
   $this->tpl['body'] .= form_hidden('id', $id);
   $this->tpl['body'] .= '<div style="text-align: right;">&lt;input type="submit" name="submit_vehicle" value="Submit" /&gt;</div>';
   $this->tpl['body'] .= '&lt;/form&gt;';
   $this->load->view('layout/blank', $this->tpl);

} [/code]

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

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

Clone this wiki locally