[eluser]Code Arachn!d[/eluser]
I've run into this when I was building my last cms and wanted the forms built dynamically. So my approach was a little lengthy but if done right you can get crazy with it.
- Step 1 involves creating a hidden field that contains all the field names in the form that you want evaluated.
- Step 2 builds the inputs along with another hidden field for each form field containing the database's column name.
- Step 3 to evaluate all the forms you take the hidden field with the field list and then loop through each one of those values which calls the field with the data.
Code:
// setup hidden fields for form open tag
$hidden = array(
'field_list' => 'ekits.ekit_title',
'call_table' => 'ekits',
'call_where' => 'ekits:WHERE ekit_id = \'' . $ekit_items['ekit_id'] . '\';|'
);
// open form do all other stuffs...
echo form_label('ekit_title','E-Kit Title*:');
echo form_hidden('ekit_title_hidden', 'page_title');
echo form_input(array(
'name' => 'ekit_title',
'id' => 'ekit_title',
'value' => $page_data['page_title'],
'class' => 'edit_fields'
));
Then to process the results when the form is submitted - I'm including what I built for the update script but this could easily be done for inserts as well it just involves formatting the sql a little differently...
Code:
function update_data()
{
$table_list = explode("|", $this->input->post('call_table', TRUE));
$field_list = explode("|", $this->input->post('field_list', TRUE));
$where_list = explode("|", $this->input->post('call_where', TRUE));
$this->db->trans_start();
foreach ($table_list as $table) {
$where_clause = '';
$sql_fields = '';
foreach ($field_list as $table_field) {
$field = explode(".", $table_field);
if($field[1] == null || $table != $field[0]) continue;
if($sql_fields != '') $sql_fields .= ', ';
// on a few occasions I need to do custom processing for field types...
switch ($this->input->post($field[1] . '_hidden', TRUE)) {
case 'article_list':
$loop_cnt = $this->input->post($field[1] . '_count', TRUE);
$set_ids = '';
for ($i = 0; $i <= $loop_cnt; $i++) {
if ($this->input->post($field[1] . '_' . $i, TRUE) == '') continue;
$set_ids .= ($set_ids != '') ? '|' : '';
$set_ids .= $this->input->post($field[1] . '_' . $i, TRUE);
}
$sql_fields .= $this->input->post($field[1] . '_hidden', TRUE) . ' = \'|'. $set_ids.'|\'';
break;
case 'office_id':
$loop_cnt = $this->input->post($field[1] . '_count', TRUE);
$set_ids = '';
for ($i = 0; $i <= $loop_cnt; $i++) {
if ($this->input->post($field[1] . '_' . $i, TRUE) == '') continue;
$set_ids .= ($set_ids != '') ? '|' : '';
$set_ids .= $this->input->post($field[1] . '_' . $i, TRUE);
}
$sql_fields .= $this->input->post($field[1] . '_hidden', TRUE) . ' = \'|'. $set_ids.'|\'';
break;
case 'practice_id':
$loop_cnt = $this->input->post($field[1] . '_count', TRUE);
$set_ids = '';
for ($i = 0; $i <= $loop_cnt; $i++) {
if ($this->input->post($field[1] . '_' . $i, TRUE) == '') continue;
$set_ids .= ($set_ids != '') ? '|' : '';
$set_ids .= $this->input->post($field[1] . '_' . $i, TRUE);
}
$sql_fields .= $this->input->post($field[1] . '_hidden', TRUE) . ' = \'|'. $set_ids.'|\'';
break;
case 'page_parent_id':
$sql_values = ($this->input->post($field[1], TRUE) != '0') ? '\''. $this->input->post($field[1], TRUE) .'\'' : 'NULL';
$sql_fields .= $this->input->post($field[1] . '_hidden', TRUE) . ' = '. $sql_values;
break;
default:
$sql_fields .= $this->input->post($field[1] . '_hidden', TRUE) . ' = \''. htmlspecialchars( $this->input->post($field[1], TRUE), ENT_QUOTES) .'\'';
break;
}
}
foreach ($where_list as $table_where) {
$where = explode(":", $table_where);
if($table != $where[0]) continue;
$where_clause = ' ' . $where[1];
}
$query = 'UPDATE ' . $table . ' SET ' . $sql_fields . $where_clause;
$this->db->query($query);
}
$this->db->trans_complete();
// due to a bug trans_status() is setup to return the reverse of the obvious
if ($this->db->trans_status() === TRUE) {
// generate an error...
$message = 'Updating ' . $this->input->post('call_table', TRUE) . ' @ ' . $this->input->post('call_where', TRUE) . ' failed miserably! Just thought you should know.';
log_message('error', $message);
return false;
} else {
return true;
}
}