Welcome Guest, Not a member yet? Register   Sign In
saving dynamic form input to a database help appreciated.
#1

[eluser]Tones_[/eluser]
Heya everyone, i'm very new to codeignighter, and i've searched the forum looking for a solution to a dilema i'm having with dynamic forms, but not quite finding what i need. so i'll try and be as detailed as i can with what i'm doing, and what i want to achieve.

First up i have a form which the field attributes are stored on the database, field name, label, validation etc. I've gotten it to pull the info from the database and done the following with the results.

Code:
$results = $this->application->get_custom_fields($program_id);

foreach ($results as $field)
{
    $this->form_validation->set_rules('custom['.$field->field_name.']', 'Input', $field->validation);
}

I've also passed the $results variable to the form in a data array.
On my form i have the following

Code:
foreach($c_fields as $field)
{
    $field_data = array(
        'name' => 'custom['.$field->field_name.']',
        'id' => 'custom['.$field->field_name.']',
        'value' => set_value('custom['.$field->field_name.']'),
        'cols' => '30',
);
    
     echo '
         <dt>
             <strong><label for="custom['.$field-&gt;field_name.']">'.$field->question.'</label>';
             if($field->validation == 'trim|required|xss_clean') { echo '*'; }
  echo'
     </strong>
            <span class="smalltext">'.$field->hint.'</span>' . form_error('custom['.$field->field_name.']') . '
         </dt>
         <dd>
             '.form_textarea($field_data).'
         </dd>';
}

All of this so far appears to do what i want it too, the issue i'm having is grabbing the submitting inputs and putting then into an array for an insert batch.

The information that is going to be saved for each input (as each input areawill take upits own row in a database table) would be as following

Code:
array(
        'applicant_id' => $insert_id, // variables already set at the start of the method from data in the url
        'program_id' => $program_id, // as above
        'variable' => '', // I need this to be the field name/id attribute
        'value' => '', // the value submitted from the form
    );

I'm stuck on taking the needed info above for each $_POST and putting it into a data array to insert as a batch like so ...

Code:
$input = array(
    array(
        'applicant_id' => $insert_id, // variables already set at the start of the method from data in the url
        'program_id' => $program_id, // as above
        'variable' => '', // I need this to be the field name/id attribute
        'value' => '', // the value submitted from the form
    );
    array(
        'applicant_id' => $insert_id, // variables already set at the start of the method from data in the url
        'program_id' => $program_id, // as above
        'variable' => '', // I need this to be the field name/id attribute
        'value' => '', // the value submitted from the form
    );
   // and so on
);

$this->db->insert_batch('tablename', $input);




Theme © iAndrew 2016 - Forum software by © MyBB