如何从表单中获取多个值并用 php 分隔它们

How to get multiple values from a form and separate them with php

不知道这种表格是怎么做出来的。我不确定这种做法是否正确。

我创建了一个从产品列表中创建交货的表单。这些产品在 SQL table.

中有一个 ID (product_id) 和数量

HTML代码

<input type="hidden" name="quantity[]"  value="{{ product.product_id }}">
<input type="text" name="quantity[]" id="quantity" value="0" placeholder="Quantity" class="form-control form-control-sm">

我需要获取该特定产品的数量值。直到这里我才知道,但我不知道如何将数量数组与 PHP 分开,以便使其像字符串一样,这样我就可以更新数据库中的产品数量。

具有以下输入的数量输出:

$quantity = $request->getParam('quantity');
var_dump($quantity);
die();

我正在使用 slim framework 3 和 eloquent

您可以使用 product_id 作为数量数组的键,这将为您提供一个格式良好的数组。

<input type="text" name="quantity[{{ product.product_id }}]" id="quantity" value="0">

这应该给你:

array (size=1)
  'quantity' => 
    array (size=2)
      'ABC' => string '0' (length=1)
      'DEF' => string '0' (length=1)

有了它,您可以:

foreach ($request->getParam('quantity') as $productId => $quantity) {
    // Create/update query.
}

要从数组中创建一个字符串,您可以使用 implode() 它的默认函数 php

例如:

$arr = array('1','2','3','4');
$arr_string = implode(" ",$arr);
echo $arr_string;

它会 return 你串起来像 1,2,3,4