简体   繁体   中英

submit form repeater values as array and loop through each

I have a form repeater like this form repeater image

which submits a code(should be same all through) a product name and a quantity in thousands. eg

  • code-1234 product-book Qty - 5000
  • code-1234 product-pen Qty -3000
  • code-1234 product-ruler Qty -2000

what i would like is to submit to db all the above data as a json with an incrementing id as per total number of items in this case 10,000 and the product name beside each id. The product name should end where its specific quantity end eg when the ids reach 5000 the next id should be 5001 and the name to begin showing pen.

I have tried this `

//array submited from form
$array = $request->get('arrayName');

foreach ($array as $key => $value) {
    $items[] = $value['no_of_items'];
    $a_sum = array_sum($items);

    $length = 15;
    $string = bin2hex(openssl_random_pseudo_bytes($length));
    $maxNumberOfItems = $a_sum;

    for ($a = 0; $a <= $maxNumberOfItems; $a++) {
        if ($a <= reset($items) && $a === $maxNumberOfItems) {
            $a = reset($items);
        }

        echo +$a . " => " . $value['item'] . "<br> ";
        $temp = bin2hex(openssl_random_pseudo_bytes($length));

        $coupons[] = [
             'id' => +$a,
             'item' => $value['item'],
             'code' => $temp,
             'item_status' => 'in-store',
         ];

    }`

It display the names correctly but does not auto increment id as explained above. The moment it reaches say 5000 the next item starts at 0 instead of 5001. Any help would be highly appreciated.

You may need to manually get the MAX(id) and do:

for ($a = $maxId; $a <= $maxNumberOfItems + $maxId; $a++)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM