Reputation: 5037
I have a view where the user will input invoice line items. When they click submit the data is sent to my controller in an array like this:
Array
(
[amount] => Array
(
[0] => 130.00
[1] => 50.00
)
[quantity] => Array
(
[0] => 1.00
[1] => 1.00
)
[item] => Array
(
[0] => 3
[1] => 4
)
[work_description] => Array
(
[0] => Programming:
[1] => Design:
)
)
Now I need to loop through each one to add it to the database via the model. Not sure if I should use foreach
or build an empty array and use array merge
?
The desired result will iterate through each array, and add to the database. So amount[0]
, quantity[0]
, item[0]
, work_description[0]
, and then on to the next set of keys.
//needs to be foreach or build array?
$items = array(
'invoice_id' => $prefix_invoices_id,
'amount' => $this->input->post('amount'),
'quantity' => $this->input->post('quantity'),
'item' => $this->input->post('item'),
'work_description' => $this->input->post('work_description'),
'taxable' => $this->input->post('taxable'),
);
$this->load->model('Invoice_item_model');
$prefix_invoice_lines = $this->Invoice_item_model->add_prefix_invoice_items($items);
And my view in case it helps (user has ability to clone rows to add more line items so it will usually come in as an array):
<tbody>
<tr class="tr_clone" id="inv_line_1">
<td>
<select id="line_item_1" name="item[]" class="invoice_line_item">
<option></option>
<?php
foreach($prefix_line_items as $line_item)
{
?>
<option value="<?php echo $line_item['id']; ?>"><?php echo $line_item['item']; ?></option>
<?php
}
?>
</select>
</td>
<td><input type="text" id="description_1" name="work_description[]" class="description" value="" /></td>
<td><input type="currency" id="amount_1" name="amount[]" class="amount" value="" /></td>
<td><input type="number" id="quantity_1" name="quantity[]" class="quantity" value="" /></td>
<td><input type="currency" id="price_1" name="price[]" class="price" value="" readonly/></td>
<td><a href="#" onclick="return false;" class="add-line-item"><i class="fa fa-plus"></i></a> <a href="#" onclick="return false;" class="remove-line-item"><i class="fa fa-minus"></i></a></td>
</tr>
</tbody>
Upvotes: 1
Views: 252
Reputation: 7111
$array = array(
'amount' => array(
0 => 130.00,
1 => 50.00
),
'quantity' => array(
0 => 1,
1 => 1
),
'item' => array(
0 => 3,
1 => 4
),
'work_description' => array(
0 => 'Programing: ',
1 => 'Design: '
)
);
$insert = array();
for ($i = 0; $i < count($array['item']); $i++)
{
$insert[$i] = array('amount' => $array['amount'][$i], 'quantity' => $array['quantity'][$i], 'item' => $array['item'][$i], 'work_description' => $array['work_description'][$i]); // $insert is ready for insert_batch
}
//var_dump($insert);
Upvotes: 1
Reputation: 2569
Well, all I can think of is this combiner:
function add_prefix_invoice_items($items = array) {
$rows = [];
$keys = ['amount', 'quantity', 'item', 'work_description', 'taxable'];
$total_keys = count($keys);
$all = $items[$keys[0]];
for ($i = 0; $i < count($all); $i++) {
$values = [];
for ($j = 0; $j < $total_keys; $j++)
$values[] = $items[$keys[$j]][$i]; // fetch next row
$row = array_combine($keys, $values); // combine key => value pairs
// can insert row ('amount' => nnn, 'quantity' => nnn, ...) into the database now
$id = ...
$rows[$id] = $row
}
return $rows;
}
Upvotes: 0