Skip to content
captainkuro edited this page Jan 10, 2013 · 35 revisions

Category:Libraries::Community | Category:Libraries::Forms Form To Table

Introduction

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

If you need help or even better have improvements let me know, please. I watch this forum thread

Usage

This will create a row with a single item and the key will be the heading:

$form['Group'] = '';

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.

$form[1]['slug'] =  '';
$form[1]['Group'] = '';

3 items:

$form[2]['slug'] =  '';
$form[2]['Group'] = '';
$form[2]['Template'] = '';

This will create the form and the table: Note the two 'not intended' lines, that is all it takes to wrap the table.

function addPage()
{
        $data =  form_open('admin/addPage', $attributes);
        $form['Title'] = '';
        $form[1]['slug'] =  '';
        $form[1]['Group'] =  form_dropdown('groups', user_groups(), 'User');
        $form['Body'] =  '';
        $form[''] =  '';
        $this->load->library('table');
        $data .= $this->table->form_table($form);
        $data .= '';
        $this->load->view('layout/blank', $data);
}

Source

/system/application/libraries/MY_Table.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 = '
'; $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 .= '
'
; return $return; } function form_table_tmpl() { return array ( 'table_open' => '', 'heading_row_start' => '', 'heading_row_end' => '', 'heading_cell_start' => '', 'row_start' => '', 'row_end' => '', 'cell_start' => '', 'row_alt_start' => '', 'row_alt_end' => '', 'cell_alt_start' => '', 'table_close' => '
', 'heading_cell_end' => '
', 'cell_end' => '
', 'cell_alt_end' => '
'
); } } ?>

Example

Here is one more sample with hidden fields and radio buttons:


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'] .= '
'
; $this->tpl['body'] .= ''; $this->load->view('layout/blank', $this->tpl); }

Clone this wiki locally