Welcome Guest, Not a member yet? Register   Sign In
Inserting post arrays into db with active record.
#1

[eluser]SaminOz[/eluser]
I'm just getting back to a site I've ignored for a while. I have a controller that works fine when it passes single value post info to a model for the creation of a new job, but fails when the post values are arrays. I am using active record syntax, and am keen to do so if poss. (code cut down, so as not to annoy).

Code:
Controller code:
$data['job'] = array(
            'job_ID' => NULL,
            'job_date' => trim($this->input->post('job_date_submit')),
            'job_number' => trim($this->input->post('job_number'))
);

Code:
Model code:
function write_new_job($job, $staff_used)
        {
            $this->db->insert('ww_job', $job);
            
        }

Now I am adding some form elements that are formatted to take multiple values like these two examples below:
Code:
<input type="text" value="00" name="startHr[]" id="startHr" class="background">
<input type="text" value="00" name="startMin[]" id="startMin" class="background">

I got errors with the previous active record insert syntax, so modified it as follows:
Code:
function write_new_job($job, $staff_used)
        {
            $this->db->insert('ww_job', $job);
            $i=0;
        
                for($i=0; $i < count($staff_used); $i++ )
                {
                    $this->db->insert('ww_staff_used', $staff_used[$i]);
                }
        }

I figured since the errors were "array to string conversion" the issue was that the array values within $staff_used, were not accessible - however this logic does not result in success Sad

Does anyone have experience with this issue and the active record class?
#2

[eluser]smilie[/eluser]
My guess is that $staff_used[$i] is not a valid array info.
I guess you should better use foreach:

Code:
function write_new_job($job, $staff_used)
        {
            $this->db->insert('ww_job', $job);
                foreach($staff_used as $val)
                {
                    $this->db->insert('ww_staff_used', $val);
                }
        }

Regards,
Smilie
#3

[eluser]SaminOz[/eluser]
Hi Smiley,

I thought I'd just update you with the code that this required (I can't help but feel this requirement is much to common not to have a 'ready made' solution).

Code:
foreach($staff_used as $key => $val)
{
    for($i = 0; $i < count($val); $i++)
    {
      $new_array[$i][$key] = $val[$i];
    }
}

foreach($new_array as $key => $value)
{
    $this->db->insert('ww_staff_used', $new_array[$key]);
}

The reason I couldn't foreach through $val as you suggested (below) is that each iteration of $val is an array itself where as $key is not. Additionally, the inputs group their arrays, whereas what I required is to have one iteration from each input array combined to input using the active record insert statement.

Code:
function write_new_job($job, $staff_used)
{
   $this->db->insert('ww_job', $job);
   foreach($staff_used as $val)
   {
      $this->db->insert('ww_staff_used', $val);
    }
}

Like I say - I'm sure there are better ways to go about this. Thanks for your suggestion.




Theme © iAndrew 2016 - Forum software by © MyBB