Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3.  
  4. class Produk extends CI_Controller
  5. {
  6.  
  7.   public function __construct()
  8.   {
  9.     parent::__construct();
  10.  
  11.     $this->load->model(array('Produk_model', 'Tags_model'));
  12.   }
  13.  
  14.   public function tambah()
  15.   {
  16.     $data['page_title']       = 'Tambah Data';
  17.  
  18.     $data['action'] = 'admin/produk/proses_tambah';
  19.  
  20.     $this->load->view('backend/produk/tambah', $data);
  21.   }
  22.  
  23.  
  24.   public function proses_tambah()
  25.   {
  26.     $this->form_validation->set_rules('nama_produk', 'Nama Produk', 'trim|required');
  27.     $this->form_validation->set_rules('harga', 'Harga Produk', 'trim|required');
  28.     $this->form_validation->set_rules('nama_tags[]', 'Tags', 'trim|required');
  29.  
  30.     $this->form_validation->set_message('required', '{field} wajib diisi');
  31.  
  32.     $this->form_validation->set_error_delimiters('<div class="alert alert-danger">', '</div>');
  33.  
  34.     if ($this->form_validation->run() == FALSE) {
  35.       $this->tambah();
  36.     } else {
  37.       $data = array(
  38.         'nama_produk' => $this->input->post('nama_produk'),
  39.         'harga'       => $this->input->post('harga'),
  40.       );
  41.  
  42.       $this->Produk_model->insert($data);
  43.  
  44.       $produk_id = $this->db->insert_id();
  45.  
  46.       $nama_tags = count($this->input->post('nama_tags'));
  47.  
  48.       for ($i = 0; $i < $nama_tags; $i++) {
  49.         $datas[$i] = array(
  50.           'produk_id' => $produk_id,
  51.           'nama_tags' => $this->input->post('nama_tags[' . $i . ']')
  52.         );
  53.  
  54.         $this->db->insert('tags', $datas[$i]);
  55.       }
  56.  
  57.       redirect('admin/produk/index');
  58.     }
  59.   }
  60. }
  61.