When I save the image and the “ordered” field to the db it saves the image(s) but each of the “ordered” fields save the same value. Any ideas on how to fix it so that the “ordered” field correctly saves what value I input for each one?
For example:
image_id | product_id | ordered | image | thumbnail -------------------------------------------------------------------- 593 | 94 | 35 | a23145b.jpg | a231458.jpg -------------------------------------------------------------------- 592 | 94 | 35 | 1348352.jpg | 1348358.jpg
Here’s what I have at the moment:
if (isset($_FILES['product_image'])) {
$files = array();
foreach ($_FILES['product_image'] as $key => $values) {
foreach ($values as $i => $value)
$files[$i][$key] = $value;
}
foreach ($files as $file) {
if (!empty($file['name'])) {
$image = $upload->upload_image($file);
$thumb = $upload->upload_image($file);
$values = array(
'product_id' => $product_id,
'image' => $image,
'thumbnail' => $thumb,
'ordered' => $_POST['ordered'][0]
);
$db->insert(config_item('cart', 'table_product_images'), $values);
}
}
}
And the js for the fields
var image_row = 0;
$('#add_image').click(function() {
html = '<tr id="image' + image_row + '">';
html += '<td><input type="file" name="product_image[' + image_row + ']" /></td>';
html += '<td><input type="text" name="ordered[' + image_row + ']" /></td>';
html += '<td><a href="#" id="remove_' + image_row + '" class="remove_image button orange">Remove</a></td>';
html += '</tr>';
$('#images table > tbody').append(html);
image_row++;
return false;
});
That’s because you are using
$_POST['ordered'][0]. The index should be dynamic. Change your code to: