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'] = '<input type="text" name="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'] =  '<input type="text" name="slug" />';
$form[1]['Group'] = '<input type="text" name="group" />';

3 items:

$form[2]['slug'] =  '<input type="text" name="slug" />';
$form[2]['Group'] = '<input type="text" name="group" />';
$form[2]['Template'] = '<input type="text" name="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'] = '<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);
}

Source

/system/application/libraries/MY_Table.php

<?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>'
            );
    }
    
}

?>

Example

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

<?php
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;"><input type="submit" name="submit_vehicle" value="Submit" /></div>';
    $this->tpl['body'] .= '</form>';
    $this->load->view('layout/blank', $this->tpl);
}
Clone this wiki locally