Zika
Zika

Reputation: 25

How to insert multiple column in codeigniter?

I want insert 2 column to database [artikel & pengarang], when I save that form it will be insert $data from to table artikel and $data to table pengarang [IDArtikel from Artikel &nama_pengarang].

This is my artikel SQL:

CREATE TABLE `artikel`(
`IDArtikel` INT(11)NOT NULL AUTO_INCREMENT,
`IDJurnal` INT(11)NOT NULL,
`IDKategori` INT(11)NOT NULL,
`judul` VARCHAR(255)NOT NULL,
`abstract` text NOT NULL,
`nama_file` VARCHAR(255)NOT NULL,
`dilihat` INT(50)NOT NULL,
`didownload` INT(50)NOT NULL,
`created_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` VARCHAR(255)NOT NULL,
`updated_time` datetime NOT NULL,
`updated_by` VARCHAR(255)NOT NULL,
PRIMARY KEY(`IDArtikel`))ENGINE = INNODB DEFAULT CHARSET = utf8;

and this is pengarang SQL:

CREATE TABLE `pengarang`(
`IDPengarang` INT(11)NOT NULL AUTO_INCREMENT,
`IDArtikel` INT(11)NOT NULL,
`nama_pengarang` VARCHAR(255)NOT NULL,
PRIMARY KEY(`IDPengarang`))ENGINE = INNODB DEFAULT CHARSET = utf8;

this is my view:

<h3><?= $title; ?></h3><?php echo form_open("admin/artikel/buat/"); ?>
<table width="95%">
    <tr>
        <td><b>Pilih Referensi Jurnal</b></td>
        <td>
            <input type="hidden" name="IDJurnal" id="IDJurnal" value="<?php echo $IDJurnal; ?>" />
            <input type="text" name="volume" id="volume" value="<?php echo $volume; ?>" readonly="readonly"  class="sedang" />
        <?php echo anchor_popup('admin/artikel/popup', 'Referensi Jurnal', array('class' => 'button')); ?>
    </td>
</tr>
<tr>
    <td><b>Kategori</b></td>
    <td>
        <?php
            echo form_dropdown('IDKategori', $kategori) . "";
        ?>
        </td>
    </tr>
    <tr>
        <td width="125"><strong>Judul</strong></td>
        <td><input type="text" name="judul" class="panjang"></td>
    </tr>
    <tr>
        <td width="125"><strong>Pengarang</strong></td>
        <td>
            <input type="text" name="pengarang" class="panjang">
        </td>
    </tr>
    <tr>
        <td><b>Abstract</b></td>
        <td>
        <?php
            $data = array('name' => 'abstract');
            echo $this->ckeditor->editor($data['name']);
        ?>
        </td>
    </tr>
    <tr>
        <td></td>
        <td>
            <input type="submit" class="button" value="Simpan">
            <input type="button" class="button" value="Batal" onClick="javascript: history.go(-1)" />
        </td>
    </tr>
<?php
            echo form_close();
?>

this is my controller:

function buat() {
    if ($this->input->post('judul')) {
        $this->MArtikel->addArtikel();
        $this->session->set_flashdata('message', 'Artikel telah di buat !');
        redirect('admin/artikel/index', 'refresh');
    } else {
        // konfigurasi ckfinder dengan ckeditor
        $this->load->library('ckeditor');
        $this->load->library('ckfinder');
        $this->ckeditor->basePath = base_url() . 'asset/ckeditor/';
        $this->ckeditor->config['toolbar'] = 'Full';
        $this->ckeditor->config['language'] = 'en';
        $this->ckfinder->SetupCKEditor($this->ckeditor, '../../../asset/ckfinder/');

        $data['title'] = "Tambah Artikel";
        $data['main'] = 'admin/artikel/artikel_buat';
        $data['jurnal'] = $this->MJurnal->getJurnalDropDown();
        $data['kategori'] = $this->MKategori->getKategoriDropDown();
        $this->load->vars($data);
        $this->load->view('dashboard/template');
    }
}

and this my model

function addArtikel() {
    $now = date("Y-m-d H:i:s");
    $data = array(
        'IDJurnal' => $this->input->post('IDJurnal'),
        'IDKategori' => $this->input->post('IDKategori'),
        'judul' => $this->input->post('judul'),
        'abstract' => $this->input->post('abstract'),
        'created_time' => $now,
        'created_by' => $_SESSION['username']
    );

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

form in pengarang, can insert multiple data

Upvotes: 0

Views: 2735

Answers (2)

Pankaj Khairnar
Pankaj Khairnar

Reputation: 3108

create new function in model addPengarang add the following code in your model, and change the values of data array as per your requirement it will insert values in Pengarng table

function addPengarang() { 
   $data = array(
        'IDPengarang' => 'value for IDPegarang',
        'IDArtikel' => 10,
        'nama_pengarang' => 'value for nama pengarang'
    );

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

Upvotes: 0

Related Questions