Welcome Guest, Not a member yet? Register   Sign In
post array of data to database table field
#1

[eluser]Brad K Morse[/eluser]
I have a series of checkboxes the user will select, named fruit[]

in the model, I have this

Code:
function insert() {
    $data = array(
        'fruit'    =>    $this->input->post('fruit[]')
    );
    
    $this->db->insert('table_name', $data);
}

It does not insert it into the table, I assume posting an array of data within this->input->post() or an array within that data array may be a no-no.

Is there some CI magic that does that for me automatically or do I need to run thru a loop and append it to a string before submitting?
#2

[eluser]umefarooq[/eluser]
you can save array in database in two ways using php handy functions serialize and json_encode here is code

Code:
with serialize

function insert() {
     $serialized = serialize($this->input->post('fruit[]'));
    $data = array(
        'fruit'    =>    $serialized
    );
    
    $this->db->insert('table_name', $data);
}

with json_encode

function insert() {
     $encoded = json_encode($this->input->post('fruit[]'));
    $data = array(
        'fruit'    =>    $encoded
    );
    
    $this->db->insert('table_name', $data);
}

when you query from database you can get your data back and can use to two more handy function to get array back again unserialize and json_decode will help you to get array back from database
#3

[eluser]redraw[/eluser]
I have same proble here..

my input text is =
Code:
<input type='text' name='trans[" + id + "][no_bukti]'>

my controller is =
Code:
foreach($_POST['trans'] as $data){
$no_bukti = serialize($this->input->post('trans[][no_bukti]'));
}

$data = array(

  'no_bukti'=>$no_bukti,

);

$this->db->insert('gl_master',$data);

but it not work..
please help.. thank you
#4

[eluser]Brad K Morse[/eluser]
try

Code:
foreach($this->input->post('trans') as $i => $value) {
  $no_bukti .= $value[$i];
}

$data = array(
  'no_bukti' => $no_bukti
);

$this->db->insert->('gl_master',$data);
#5

[eluser]redraw[/eluser]
thanks for the fast respons. but it still not work
#6

[eluser]Brad K Morse[/eluser]
Rename your text input to:

Code:
<input type='text' name='trans[]'>

Then try this:

Code:
foreach($this->input->post('trans') as $i => $value) {
  $no_bukti .= $value[$i];
}

$data = array(
  'no_bukti' => $no_bukti
);

$this->db->insert->('gl_master',$data);

This should work, but I see you're putting an index '+ id +', I'm guessing this is from some javascript, but just try this first, see if it inserts successfully, then try revising to get it work w/ your javascript dynamically naming the text fields.
#7

[eluser]redraw[/eluser]
i did actually as you say. but when i try to save it into database, i get this eror = Undefined offset: 1 on line this code
Code:
$no_bukti .= $value[$i];
and i have this error too = Message: Undefined variable: no_bukti

the strange is no bukti still save in my table, but it only show the 3rd char that i've input.
example :
i input "NOT001"
it save in my table = "T" (3rd char from NOT001)
#8

[eluser]Brad K Morse[/eluser]
do a print_r for trans[]

Code:
print_r($this->input->post('trans'));
and paste what it displays
#9

[eluser]redraw[/eluser]
the value of my input is = NOT003
it's show = Array ([2] => NOT003)
#10

[eluser]Brad K Morse[/eluser]
You only have one input field for trans[]?




Theme © iAndrew 2016 - Forum software by © MyBB