Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a large form where 3 arrays are sent to the controller from the form: product_code | quantity | cost.

$id = $request->get('product_code');   // GRAB PRODUCT DATA FROM POST ARRAY
$qty = $request->get('quantity');   // GRAB QUANTITY DATA FROM POST ARRAY
$cost = $request->get('cost');      // GRAB COST DATA FROM POST      

The output from the request on all three arrays is here: PasteBin

My problem is I can not figure out how best to loop through each of these three arrays so that I can insert into MySQL the values in the correct order. I have tried both a foreach, a nested foreach and a for loop and I have not managed yet to get all three values inserting onto a single row.

I don't think the HTML is very relevant, but here is a sample anyway:

<div class="well form-group  ">
    <div class="col-sm-2 col-md-2">
        <label for="nails_staples">Nails &amp; Staples:</label>
    </div>
    <div class="col-sm-4 col-md-4">
        <select class="form-control product_id" name="product_code[10]">
            <option selected="selected" value="">None</option>
            <option value="8769">1 1/4 Coil Nails - box | $26.00</option>
            <option value="6678">2&quot; Hot Dipped Shake Nails | $135.00</option>
        </select>
    </div>
    <div class="col-sm-1 col-md-1">
        <label for="nails_req">Quantity:</label>
    </div>
    <div class="col-sm-2 col-md-2">
        <input class="form-control quantity" name="quantity[10]" type="text">
    </div>
    <div class="col-sm-1  col-md-1">
        <label for="cost">Cost:</label>
    </div>
    <div class="col-sm-2 col-md-2">
        <input class="form-control cost" readonly="readonly" name="cost[]" type="text">
    </div>
</div>
share|improve this question
    
are you trying to save them into one field or separate fields? – Can Celik Mar 2 '16 at 0:09
    
I'm guessing you're having issues with the order of the items in the array being preserved. Your product_code array looks like a <select> tag, which is handled strangely in different browsers for multiple selections, and the sort order can change. Can you give an example of input data for the 3 arrays, or the whole form? Maybe dd($request); in your controller and post it to pastebin? – QuickDanger Mar 2 '16 at 0:10
    
@QuickDanger Thank you for looking at this. I swapped out the HTML withy compiled HTML so you can see it better. Yes, my problem is that a foreach loop is designed to take an array and split out two values: the key and the value. My problem is that I have 3 arrays all with a key value pair plus a customer id, and I need to reassemble them somehow so that I can do a DB insert of the customer_id | product_id | quantity | cost I have tried all different kinds of foreach loop. I have tried combining the arrays, tried a for loop. I just can't get it. – Vince Mar 2 '16 at 17:50
    
@CanCelik Thank you for looking at this. I hopefully clarified things a little in my comment above. – Vince Mar 2 '16 at 17:51
    
@QuickDanger Paste bin with outputs is now added. Thanks ! – Vince Mar 2 '16 at 18:37
up vote 1 down vote accepted

There are a few issues with this approach. Ideally you'd have separate calls to a controller for each entity you want to change, using AJAX to make each individual call as the user changes the entity. This solves the issue of attributes not being strongly tied to the entity ID (in your case, product_code).

If you're willing to make the assumption that all of the arrays match up, you should at least check that they are the same length.

Obviously I don't know all the specifics of your Model or exactly what you're looking for, but based on your paste bin, this should work.

$id = $request->get('product_code');   // GRAB PRODUCT DATA FROM POST ARRAY
$qty = $request->get('quantity');      // GRAB QUANTITY DATA FROM POST ARRAY
$cost = $request->get('cost');         // GRAB COST DATA FROM POST   

$count_ids = count($id);
if(count($qty) != $count_ids || count($cost) != $count_ids) throw new Exception("Bad Request Input Array lengths");
for($i = 0; $i < $count_ids; $i++){
    if (empty($id[$i])) continue; // skip all the blank ones
    $newProduct = new Product();
    $newProduct->code = $id[$i];
    $newProduct->qty = $qty[$i];
    $newProduct->cost = $cost[$i];
    $newProduct->save();
}
share|improve this answer
    
It's worth mentioning that if $id[$i] == 0, it is considered "empty" and skipped. – QuickDanger Mar 2 '16 at 20:05
  1. You did not pass any example of how your loops look like.
  2. Do not paste the template, paste rendered html, this says not much to most php developers (not working with laravel). I do not see even the form tags.
share|improve this answer
    
My bad - I replace the html with compiled html. – Vince Mar 2 '16 at 17:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.