I've found similar questions but, I'm failing to find any answers that works for my situation.
Using Codeigniter: I have a db table for products and a db table for inventory. I want to display an html table on my site, each row being a product. I've included an image to help explain.
Upon submission, I would like to insert each product row of data as an individual row in the database.
This is my controller at its most simplest form...
CONTROLLER----------------------------------
function add_show_inventory()
{
$data['products']=$this->product->get_products();
$this->form_validation->set_rules('skuID', 'skuID', 'required');
$this->form_validation->set_rules('brought-in', '"Brought In"', 'required');
$this->form_validation->set_rules('finished-with', '"Finished With"', 'required');
$this->form_validation->set_rules('deliveries', 'Deliveries');
$this->form_validation->set_rules('add-1', 'Add 1');
$this->form_validation->set_rules('add-2', 'Add 2');
$this->form_validation->set_rules('band-comp', 'Band Comp');
$this->form_validation->set_rules('other-comp', 'Other Comp');
$this->form_validation->set_rules('half-sales', 'Half Sales');
$this->form_validation->set_rules('half-amount', 'Half Amount');
$this->form_validation->set_error_delimiters('<div class="error-module">', '</div>');
if($this->form_validation->run()== FALSE){
$this->load->view('header');
$this->load->view('menu');
$this->load->view('inventory/show-add-inventory', $data);
$this->load->view('footer');
} else {
if($_POST){
$data=array(
'brought-in'=>$_POST['brought-in'],
);
$this->inventory->insert_inventory($data);
redirect(base_url().'shows/');
}
}
}
GET PRODUCTS MODEL -------------------------------
function get_products($num=10,$start=0) {
$where= array('userID'=>$this->session->userdata('userID'), 'active'=>1);
$this->db->select()
->from('products')
->where($where)
->order_by('product','asc')
->limit($num,$start);
$query=$this->db->get();
return $query->result_array();
}
INSERT INV MODEL------------------------------------------
function insert_inventory($data) {
$product_count= $this->db->count_all('products');
for ($i=0; $i < $product_count; $i++) {
$this->db->insert('inventory', array('brought-in'=>$_POST['brought-in']));
}
return $this->db->insert_id();
}
VIEW ------------------------------------------
<form action="<?= base_url(); ?>inventories/add_show_inventory" method="post">
<?php if(!isset($products)){ ?>
<p>There are currently no products posted yet.</p>
<?php } else {
foreach($products as $row){
?>
<td class="highlight">
<input type="text" name="brought-in" value="0"> <!-- Brought In -->
</td>
<?php } ?>
<input type="submit" name="submit" class="submit">
The results I get from this code here is Three entrees to the DB which is what I want but all of the rows are identical to the last row in the form... meaning the iteration in the model is only iterating over that last row on the form...
Again that code is stripped down to its simplest form possible so I don't have 3 full pages of code... but if you would like to see anything I am not showing please let me know...
Any and all help much appreciated... I've been working on this for a couple weeks...