Last active
October 20, 2017 01:30
-
-
Save steveosoule/0250ae966224c20862a8e51d04cccd8c to your computer and use it in GitHub Desktop.
CodeIgniter - Table Form Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?= 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