如何从一个表单中获取多个值并用php将它们分隔开
我不知道这种形式是怎么形成的。我不确定这种方法是否正确 我创建了一个表单,从产品列表中创建交付。这些产品在SQL表中有一个id(product_id)和数量 HTML代码如何从一个表单中获取多个值并用php将它们分隔开,php,mysql,arrays,eloquent,slim,Php,Mysql,Arrays,Eloquent,Slim,我不知道这种形式是怎么形成的。我不确定这种方法是否正确 我创建了一个表单,从产品列表中创建交付。这些产品在SQL表中有一个id(product_id)和数量 HTML代码 <input type="hidden" name="quantity[]" value="{{ product.product_id }}"> <input type="text" name="quantity[]" id="quantity" value="0" placeholder="Quanti
<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">
我使用的是slim framework 3和Elount您可以使用产品id作为quantity数组的键,该数组将为您提供一个格式良好的数组
<input type="text" name="quantity[{{ product.product_id }}]" id="quantity" value="0">
有了这些,您可以:
foreach ($request->getParam('quantity') as $productId => $quantity) {
// Create/update query.
}
要从数组中生成字符串,可以使用php的默认函数
infrade()
例如:
$arr = array('1','2','3','4');
$arr_string = implode(" ",$arr);
echo $arr_string;
它将返回字符串,类似于1,2,3,4
序列化所有内容,然后您可以使用正则表达式手动生成字符串。我不确定您将在表单中得到什么字符串,但这是您可以做到的。请向我们展示数量数组的输出/转储。@Waterlomatt我刚刚用输出更新了我的问题。谢谢。对不起,这样做,而不是有一个额外的隐藏输入叶-你可以删除隐藏的输入。我编辑了我的答案。
foreach ($request->getParam('quantity') as $productId => $quantity) {
// Create/update query.
}
$arr = array('1','2','3','4');
$arr_string = implode(" ",$arr);
echo $arr_string;