5

I want to insert multiple data (PHP CodeIgniter) using array. but I still find an error. The error is array is not clear

function insert(){

    $hitung=count($_POST['pembicara']);

    $pengisi=implode(',',$_POST['pembicara']);
    $materi=implode(',',$_POST['materi']);
    $alasan=implode(',',$_POST['alasan']);

    $datapembicara = array(
    'id_kegiatan'   => $x,
    'nama_pembicara'   => $pengisi,                                         
    'materi'      => $materi,                                           
    'alasan'      => $alasan,
    );

    $this->m_admin_smf->add_pembicara($datapembicara);
}

when I var_dump show the result like

array (size=4)
  'id_kegiatan' => int 990550
  'nama_pembicara' => string '1,2' (length=9)
  'materi' => string '1,2' (length=3)
  'alasan' => string '1,2' (length=3)

array (size=4)
  'id_kegiatan' => int 990550
  'nama_pembicara' => string '1,2' (length=9)
  'materi' => string '1,2' (length=3)
  'alasan' => string '1,2' (length=3)

the result is should be like

array (size=4)
  'id_kegiatan' => int 990550
  'nama_pembicara' => string '1' (length=9)
  'materi' => string '1' (length=3)
  'alasan' => string '1' (length=3)

array (size=4)
  'id_kegiatan' => int 990550
  'nama_pembicara' => string '2' (length=9)
  'materi' => string '2' (length=3)
  'alasan' => string '2' (length=3)

what should I do?

How if like this? but show an error

$hitung = count($_POST['pembicara']);
$datapembicara = array();
for($i = 0; $i < $hitung; $i++) {
    $datapembicara[] = array(
        'id_kegiatan' => $x,
        'nama_pembicara' => $_POST['pembicara'][$i],
        'materi' => $_POST['materi'][$i],
        'alasan' => $_POST['alasan'][$i],
    );
}

$this->m_admin_smf->add_pembicara($datapembicara);

1 Answer 1

2

If you want them on a series of batches, then you'll have to do something other than implode since the behavior will be different from the one you need. You can use a simple for loop to create such array structure into series of batches:

// Controller
function insert()
{

    $hitung = count($_POST['pembicara']);
    $datapembicara = array();
    for($i = 0; $i < $hitung; $i++) {
        $datapembicara[] = array(
            'id_kegiatan' => $x,
            'nama_pembicara' => $_POST['pembicara'][$i],
            'materi' => $_POST['materi'][$i],
            'alasan' => $_POST['alasan'][$i],
        );
    }

    var_dump($datapembicara);

    $this->m_admin_smf->add_pembicara($datapembicara);
}

Sidenote: I'd suggest use input class of codeigniter since it can handle XSS for you.

$value = $this->input->post('value', true); // add true parameter flag

Then after said array is made, you can use ->insert_batch() of active record. It'll do the multiple insertions for you, plus, your values are automatically escaped.

// Model
public function add_pembicara($data)
{
    $this->db->insert_batch($data);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Okay Its work,but If I want to insert into database , its show an error like ----INSERT INTO kegiatan_pembicara (0, 1, 2) VALUES (Array, Array, Array)----
@Fatchul i revised some parts of my answer, so that you'd be able to use your newly created array, into insert_batch() inside your model
@Fatchul by the way, could you also post your view files, post the form also, along with var_dump($_POST)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.