Welcome Guest, Not a member yet? Register   Sign In
update from array values
#1

[eluser]Kamusksn[/eluser]
Hello, I have a form with a static field "id" and 'N' number of fileds quantity[] & description[]


I'm gettin the values on my model
Code:
$data['id'] = $this->input->post('id');
$data['quantity'] = $this->input->post('quantity');
$data['description'] = $this->input->post('description');


DATABASE FIELDS:
Code:
id
quantity
description

e.g. I have an array ($data) with the following content
Code:
ID-XXX
Array ( [0] => 1 [1] => 5 )
Array ( [0] => Lock [1] => Key  )

How can I update my DB with the new information
Code:
$this->db->where("ID and Description match array information");
$this->db->update('products', $array_here);
#2

[eluser]CroNiX[/eluser]
One way would be to loop over one of the arrays to get the index, and then use that index to get the values from the other related arrays at the same index point.
Code:
foreach($data['id'] as $index => $id)
{
  $save_data = array(
    'quantity' => $data['quantity'][$index],
    'description' => $data['description'][$index]
  );
  $this->db
    ->where('id', $id)
    ->update('products', $save_data);
}
#3

[eluser]Kamusksn[/eluser]
Got it, now I have a trouble since yesterday...

I have the input checkbox "delete[]" to delete the row if is check, or update if not.

I'm looking that if I don't mark the checkbox there is no value to pass to the array that means.

I have 3 rows, I select one checkbox my

Code:
$data['delete'] = $this->input->post('delete');

will show this ouput, skipping the other values

Quote:Array (0 => on)

What I want to do is use isset to check every item on the array delete[] and if not has value, give them a value of 0. how can I do that or what is the best way to handle this on codeigniter?
#4

[eluser]Kamusksn[/eluser]
I have solve the problem with the help of freshpixel by IRC, he told me give the id value to every checkbox, to use as index for delete.

so, first I'm checking if ($this->post->('delete') is true, if it's true

I count the array items (marked rows for delete), I select the total rows for the DB and if
marked rows => total rows, I run a query to delete records from 2 tables if not

I use foreach to go through marked deletes and delete items just from 1 table.





Theme © iAndrew 2016 - Forum software by © MyBB