[eluser]Unknown[/eluser]
I have to build a lot of large forms for work, most have 30-50 fields each, and grew very tired of creating the views for these large forms so I came up with this helper. It's not perfect and I wouldn't advise using it on smaller forms, but for large ones, I would rather write 10-15 lines of code versus 60-70 lines of code to build the form.
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//abstract function to create a 2 column form (make sure your style creates a two-column form or alter as needed)
//Created by mike morey [email protected]
//FREE to use, amend, modify, and distribute as long as this info remains intact
function create_dbform($data, $controller, $js = '', $dropdowns = null, $textareas = null, $excludes = null, $hidden = null, $required = null, $dates = null)
{
$form = form_open(base_url().$controller).'<br />';
foreach($data as $key => $value){
if(in_array($key, $excludes) && !in_array($key, $hidden)){
unset($data[$key]);
}
if(in_array($key, $hidden)){
$form .= form_hidden($key, $value);
unset($data[$key]);
}
}
$x=0;
foreach($data as $key => $value){
if($x % 2){
//
}else{
$form .= '<tr>';
}
if(array_key_exists($key, $dropdowns)){ //DROPDOWNS
$form .= '<td';
if(in_array($key, $required)){ $form .= ' bgcolor="#F00"><span color:#FFFFFF;">';
}else{
$form .= '><span ">';
}
$fjs = 'id="'.$key.'"';
$form .= ucwords(str_replace('_', ' ', $key)).': </span><span >'.form_dropdown($key, $dropdowns[$key], $value, $fjs).'</span></td>';
}elseif(in_array($key, $textareas)){ //TEXTAREAS
$area = array(
'name' => $key,
'id' => $key,
'value' => $value,
'rows' => '4', //EDITABLE
'cols' => '39', //EDITABLE
);
$form .= '<td><span ">'.ucwords(str_replace('_', ' ', $key)).': </span><span float:right;">'.form_textarea($area).'</span></td>';
}elseif(in_array($key, $dates)){ //DATE FIELDS
$form .= '<td';
if(in_array($key, $required)){ $form .= ' bgcolor="#F00"><span color:#FFFFFF;">';
}else{
$form .= '><span ">';
}
$caljs = 'class="datefield" id="'.$key.'"';
$form .= ucwords(str_replace('_', ' ', $key)).': </span><span ">'.form_input($key, $value, $caljs).'</span></td>';
}else{ //TEXT FIELDS
$text = array(
'name' => $key,
'id' => $key,
'value' => $value,
'maxlength' => '75', //EDITABLE
'size' => '40', //EDITABLE
);
$form .= '<td';
if(in_array($key, $required)){ $form .= ' bgcolor="#F00"><span color:#FFFFFF;">';
}else{
$form .= '><span ">';
}
$form .= ucwords(str_replace('_', ' ', $key)).': </span><span float: right;">'.form_input($text).'</span></td>';
}
if($x % 2){
$form .= '</tr>';
}else{
//
}
$x++;
}
$form .= '<div id="hidden_formdata" >'; //For use with JS if needed to display under certain triggers
$form .= '</div>';
$form .= '<tr><td colspan="2"><div align="center">'.form_submit('submit', 'Submit').'</div></td></tr><br /><br />'; //EDITABLE Value
$form .= form_close().'<br />';
return $form;
}
Controller Example:
Code: $info = $this->net_model->get_client($clientid);
$hidden = array('id');
$excludes = array('updated','id','noweb','contract','abbreviation');
$dropdowns = array('category_id' => get_categories(),'subcategory' => get_subcategories(), 'foodcourt' => array('0' => 'No', '1' => 'Yes'), 'is_restaurant' => array('0' => 'No', '1' => 'Yes'), 'rest_type' => array('' => 'Choose', 'sit_down' => 'Sit Down', 'stand_up' => 'Stand Up'), 'does_delivery' => array('0' => 'No', '1' => 'Yes'), 'cctype' => array('' => 'Choose', 'AMEX' => 'Amex', 'DISC' => 'Discover', 'MC' => 'MasterCard', 'VISA' => 'Visa'));
$textareas = array('notes', 'foodcourt_exclusives', 'foodcourt_notes', 'foodcourt_deals');
$required = array('firstname', 'lastname', 'email', 'account', 'category_id', 'subcategory');
$dates = array();
$data['clientinfo'] = create_dbform($info, 'orders/update/'.$clientid, '', $dropdowns, $textareas, $excludes, $hidden, $required, $dates);
All parameters except the $js parameter are passed as arrays, $dropdowns is a multi-dimensional array of keys with values consisting of the dropdown options.
Note: The get_categories() and get_subcategories() functions are custom helpers I made to return an array of these items, you can do similar.
DEFINITIONS:
$info - the form data returned from query
$hidden - any hidden fields for your form
$excludes - any fields you want omitted from your form (used only when doing an SELECT * query)
$dropdowns - multi-dimensional array for your dropdowns data
$textareas - fields for text area input
$required - any fields you want to be styled differently as being required
$dates - any date fields (this required that you have a version of jQuery UI loaded in your page header to work and use the following code in your header :
Code: [removed]
$(function() {
$(".datefield").each(function() {
$(this).datepicker();
});
});
[removed]';
The styling in the helper is modifiable to fit your own CSS styling, mine is basic.
Enjoy.
[eluser]quickshiftin[/eluser]
Nice helper!
|