I start with the following:
<form action="" method="post" enctype="multipart/form-data" id="form">
<?php foreach($attributes as $attribute){ ?>
<input type="checkbox" name="attribute[][attr_id]" value="<?php echo $attribute['attribute_id']; ?>">
<input type="text" name="attribute[][attr_name]"> value="<?php echo $attribute['attribute_name']; ?>">
<?php } ?>
</form>
So each $attribute has a checkbox and a text input; whenever someone would check (one or more boxes) and would insert text (only to the checked items) I want to get in the DB the [attr_id]
and the [attr_name]
for the specific item.
So I continue with the following:
if(isset($_POST['attribute'])){
foreach($_POST['attribute'] as $attribute){
$attr_id = $attribute['attr_id'];
$attr_name = $attribute['attr_name'];
"INSERT INTO " . DB_PREFIX . "attribute_xref SET productid = '" . $productid . "', attribute_id='". $attr_id ."', attribute_name='" . $attr_name . "'";
}
}
But, the result is a little different as of I would have expected. Every time a box is checked and its text input is typed, their values are sent to two different DB rows:
productid -- attribute_id -- attribute_name
10 -- 102 -- empty
10 -- 0 -- somename
On the above second row the attribute_id has zero value for not being checked.
I cannot get the whole picture where is my mistake.
Finally. The tricky answer was to add an identical numerical index to the associative inputs like this:
<form action="" method="post" enctype="multipart/form-data" id="form">
<?php foreach($attributes as $attribute){ ?>
<input type="checkbox" name="attribute[i][attr_id]" value="<?php echo $attribute['attribute_id']; ?>">
<input type="text" name="attribute[i][attr_name]"> value="<?php echo $attribute['attribute_name']; ?>">
<?php } ?>
</form>
where "i" in my case would take the variable number from 'attribute_id':
<input type="checkbox" name="attribute[<?php echo $attribute['attribute_id']; ?>][attr_id]" value="<?php echo $attribute['attribute_id']; ?>">
<input type="text" name="attribute[<?php echo $attribute['attribute_id']; ?>][attr_name]"> value="<?php echo $attribute['attribute_name']; ?>">
Hopefully my answer would help somebody in the future, too.