Welcome Guest, Not a member yet? Register   Sign In
How to insert multiple rows from a single view.
#1

[eluser]FlingeR[/eluser]
Hi, i have a table with sellers and I need to create a row in another table containing the ID, Number of sales and Number of clients.

So, the Controller gives me a $query with all the IDs and SELLERs, with this I need to select in the view the name of the "SELLER" (drop-down field) and also specify the "Number of sales" and the "Number of Clients" and finally send all this to the DB.

It might be needed insert any number of SELLERS at once.

please... help... Big Grin

I understand how to get the query from MySQL, but i don't know how to identify and work with multiple fields from the same type (many sellers).
#2

[eluser]pickupman[/eluser]
Feel free to post some code of what you may have. Here is something you find useful.
Code:
//$sellers is query returned from DB
$sellers_dropdown = array();

if($sellers->num_rows() > 0){
  
  foreach($sellers->result() as $seller){
    $sellers_dropdown[$seller->seller_id] = $seller->seller_name; //$seller->field_name
  }
}

echo form_open('sellers/update');
echo form_label('Sellers', 'sellers');
echo form_dropdown('sellers', $sellers_dropdown); //Create dropdown for sellers name
echo form_label('Number of Sales', 'sales');
echo form_input('sales'); //Create text input box for number
echo form_submit('submit', 'Update');
echo form_close();

//In sellers/update controller method
function update(){
  $this->load->library('form_validation');
  $this->form_validation->set_rules('sellers', 'sellers name', 'required|trim');
  $this->form_validation->set_rules('sales', 'number of sales', 'required|is_numeric');
  
  if($this->form_validation->run()){
    $sellers_id = $this->input->post('sellers'); //set sellers id
    $data['sellers_sales'] = $this->input->post('sales'); //set sales input field

    $this->db->where('sellers_id', $sellers_id); //set row to be updated in DB
    $this->db->update('sellers', $data); //update row in DB

    redirect('sellers/update'); //redirect back to same page
  }
}
#3

[eluser]FlingeR[/eluser]
Hi, thanks for the help.
But I need to insert 20 or more sellers in one click, with that code i only can insert one at a time, and that's the matter. Big Grin
#4

[eluser]bproctor[/eluser]
I'm not sure I understand exactly, but it sounds like you have a form with multiple rows, and three columns, the seller, number of sales and number of clients and you want to fill all these out and click submit and have all those rows be inserted into the database...

Code:
&lt;?php for ($i = 0; $i < 20; $i++) : ?&gt;
   <select name="seller[]">
      &lt;?php foreach ($sellers as $seller) : ?&gt;
         <option value="&lt;?php echo $seller->id; ?&gt;">&lt;?php echo $seller->name; ?&gt;</option>
      &lt;?php endforeach ?&gt;
   </select>
   &lt;input type="text" name="num_sales[]" /&gt;
   &lt;input type="text" name="num_clients[]" /&gt;
&lt;?php endfor ?&gt;

Then simply insert the entire post array into the database...

Code:
$this->db->insert_batch('my_table', $this->input->post());

Or if you need to have finer control over the data going into the database you could run it though a loop, something like this...

Code:
$post = $this->input->post();
for ($i = 0; $i < count($post['seller']; $i++) {
   $this->db->insert('my_table', array('seller_id' => $post['seller'][$i], 'num_sales' => $post['num_sales'][$i], 'num_clients' => $post['num_clients'][$i]));
}
#5

[eluser]FlingeR[/eluser]
Thanks to @bproctor and @pickupman for the help !




Theme © iAndrew 2016 - Forum software by © MyBB