Skip to content

Instantly share code, notes, and snippets.

@steveosoule
Last active October 20, 2017 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveosoule/0250ae966224c20862a8e51d04cccd8c to your computer and use it in GitHub Desktop.
Save steveosoule/0250ae966224c20862a8e51d04cccd8c to your computer and use it in GitHub Desktop.
CodeIgniter - Table Form Helper
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Sandbox extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function table_form($table, $id)
{
$this->load->model('utility_model');
$data = [
'form_elements' => $this->utility_model->table_record_to_form($table, $id)
];
$this->load->view('table-form', $data);
}
}
/* End of file sandbox.php */
/* Location: ./application/controllers/sandbox.php */
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Utility_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function table_record_to_form($table, $id)
{
$results = new stdClass();
// Get Data
$this->db->where('id', $id);
$query = $this->db->get($table);
$data = $query->result();
$data = $data[0];
// Get Structure
$query = $this->db->query('DESCRIBE '.$this->db->escape_str($table) );
$columns = $query->result();
$form_elements = [];
// var_dump($columns);
// Match Structure to Data Values
foreach ($columns as &$column)
{
// Get Data Value
$column->Value = NULL;
if( isset($data->{$column->Field}))
{
$column->Value = $data->{$column->Field};
}
// Format Form Strucutre
$new_form_element = [
'label' => humanize($column->Field),
'name' => $column->Field,
'value' => $column->Value,
'class' => 'form-control',
'type' => 'text'
];
if( $column->Field === 'id' )
{
$new_form_element['type'] = 'hidden';
}
else if( $column->Type === 'text' )
{
$new_form_element['type'] = 'textarea';
$new_form_element['rows'] = '5';
}
elseif( $column->Type === 'date' )
{
$new_form_element['type'] = 'date';
}
elseif( preg_match_all("/(\w+)\((\d+),(\d+)\)/", $column->Type, $matches) )
{
if( count($matches) === 4 )
{
if( $matches[1][0] === 'decimal' )
{
$new_form_element['type'] = 'number';
$new_form_element['maxlength'] = $matches[2][0];
if( $matches[3][0] == "2" ){
$new_form_element['step'] = '0.01';
}
}
}
}
elseif( preg_match_all("/(\w+)\((.*)\)/", $column->Type, $matches) )
{
if( count($matches) === 3 )
{
if( $matches[1][0] === 'varchar' )
{
$new_form_element['maxlength'] = $matches[2][0];
}
else if( $matches[1][0] === 'int' )
{
if( $matches[2][0] == "1" ){
$new_form_element['type'] = 'checkbox';
$new_form_element['class'] = NULL;
$new_form_element['value'] = '1';
if( $column->Value == '1' )
{
$new_form_element['checked'] = 'checked';
}
}
else
{
$new_form_element['type'] = 'number';
$new_form_element['maxlength'] = $matches[2][0];
$new_form_element['step'] = 1;
}
}
}
}
$form_elements[] = $new_form_element;
}
return $form_elements;
}
}
/* End of file Utility_model.php */
/* Location: ./application/models/Utility_model.php */
<?= form_open() ?>
<? foreach ($form_elements as $form_element): ?>
<label>
<?= $form_element['label']; ?>
<?= form_input( $form_element ) ?>
</label>
<? endforeach ?>
<button class="btn btn-primary btn-raised">Submit</button>
<?= form_close(); ?>
<pre style="display: none"><?= var_export($form_elements) ?></pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment