Reputation: 1398
I have a form where the user can add all the additional fields that they need.
Note: I would like to find a way to organize the code more efficiently, I will explain it in detail later.
As it is structured in HTML, I have simplified it to make it easier to understand:
<form action="" method="post">
<h1>Products</h1>
<p>
<input type="text" name="product_name[]" value="Product #1">
<input type="text" name="product_sku[]" value="pro-001">
<input type="text" name="product_price[]" value="$12.00">
<input type="text" name="product_stock[]" value="10">
</p>
<p>
<input type="text" name="product_name[]" value="Product #2">
<input type="text" name="product_sku[]" value="pro-002">
<input type="text" name="product_price[]" value="$12.00">
<input type="text" name="product_stock[]" value="10">
</p>
<p><button type="submit">Add Product</button></p>
</form>
I need to process these received data, to later work with them more easily, for example adding it to the database. But I get the code this way, a structure that doesn't make things much easier for working with that data.
Array
(
[product_name] => Array
(
[0] => Product #1
[1] => Product #2
)
[product_sku] => Array
(
[0] => pro-001
[1] => pro-002
)
[product_price] => Array
(
[0] => $12.00
[1] => $12.00
)
[product_stock] => Array
(
[0] => 10
[1] => 10
)
)
I wish I could receive the code like this:
Array
(
[0] => Array
(
[product_name] => Product #1
[product_sku] => pro-001
[product_price] => $12.00
[product_stock] => 10
)
[1] => Array
(
[product_name] => Product #2
[product_sku] => pro-002
[product_price] => $12.00
[product_stock] => 10
)
)
I have achieved it in the following way, but I want to do it in a more optimal way.
if(isset($_POST) && !empty($_POST)) {
// Total products to add
$total_products = count($_POST["product_name"]);
// Products ordered
$products_created = [];
for ($i=0; $i <$total_products ; $i++) {
$products_created[$i] = array(
'product_name' => $_POST["product_name"][$i],
'product_sku' => $_POST["product_sku"][$i],
'product_price' => $_POST["product_price"][$i],
'product_stock' => $_POST["product_stock"][$i]
);
}
echo "<pre>"; print_r($_POST);
echo "<pre>"; print_r($products_created);
}
Complete example code:
<?php
if(isset($_POST) && !empty($_POST)) {
// Total products to add
$total_products = count($_POST["product_name"]);
// Products ordered
$products_created = [];
for ($i=0; $i <$total_products ; $i++) {
$products_created[$i] = array(
'product_name' => $_POST["product_name"][$i],
'product_sku' => $_POST["product_sku"][$i],
'product_price' => $_POST["product_price"][$i],
'product_stock' => $_POST["product_stock"][$i]
);
}
echo "<pre>"; print_r($_POST);
echo "<pre>"; print_r($products_created);
}
?>
<form action="" method="post">
<h1>Products</h1>
<p>
<input type="text" name="product_name[]" value="Product #1">
<input type="text" name="product_sku[]" value="pro-001">
<input type="text" name="product_price[]" value="$12.00">
<input type="text" name="product_stock[]" value="10">
</p>
<p>
<input type="text" name="product_name[]" value="Product #2">
<input type="text" name="product_sku[]" value="pro-002">
<input type="text" name="product_price[]" value="$12.00">
<input type="text" name="product_stock[]" value="10">
</p>
<p><button type="submit">Add Product</button></p>
</form>
Upvotes: 0
Views: 72
Reputation: 350756
You could use a transpose
function, that will turn your $_POST
data into the desired data structure:
function transpose($array) {
foreach ($array as $key => $values) {
foreach ($values as $i => $value) $result[$i][$key] = $value;
}
return $result;
}
call as:
$products_created = transpose($_POST);
You could get the desired structure directly in PHP $_POST
, if you change your HTML to this:
<form action="" method="post">
<h1>Products</h1>
<p>
<input type="text" name="product[0][name]" value="Product #1">
<input type="text" name="product[0][sku]" value="pro-001">
<input type="text" name="product[0][price]" value="$12.00">
<input type="text" name="product[0][stock]" value="10">
</p>
<p>
<input type="text" name="product[1][name]" value="Product #2">
<input type="text" name="product[1][sku]" value="pro-002">
<input type="text" name="product[1][price]" value="$12.00">
<input type="text" name="product[1][stock]" value="10">
</p>
<p><button type="submit">Add Product</button></p>
</form>
You would need some logic to inject that sequential number in the first bracket pairs in the name
attributes. Assuming that this HTML is produced by PHP, it would not be hard to do that. If your HTML is dynamic on the client side, where rows can be added without interaction with the server, then you'll need to do this (also) in JavaScript.
I suppose you'll have no problem in setting that up.
Upvotes: 1