1

I have prepared the form to be inputted to the database, but specifically for multiple checkboxes. I've found a similar case with the solutionbut not with the algorithm that I use

Here it is my controller

class Ruang extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->model("m_ruang");
        $this->load->library('form_validation');
        if($this->session->userdata('status') != "login"){
            redirect(base_url("login"));
        }
    }

    public function index()
    {
        $data["ruang"] = $this->m_ruang->getAll();
        $this->load->view('admin/ruang/index.php', $data);
    }

    public function add()
    {
        $ruang = $this->m_ruang;
        $validation = $this->form_validation;
        $validation->set_rules($ruang->rules());

        if ($validation->run()) {
            $ruang->save();
            $this->session->set_flashdata('success', 'Berhasil ditambahkan');
        }   

        $this->load->view("admin/ruang/add_ruang");
    }

Here it is my models

class M_ruang extends CI_Model
{
    private $_table = "ruang";

    public $id_ruang;
    public $ruang;
    public $kapasitas_kuliah;
    public $kapasitas_ujian;
    public $layout;
    public $fasilitas;

public function getAll()
    {
        return $this->db->get($this->_table)->result();
    }

    public function getById($id)
    {
        return $this->db->get_where($this->_table, ["id_ruang" => $id])->row();
    }

    public function save()
    {
        $post = $this->input->post();
        $this->id_ruang = uniqid();
        $this->ruang = $post["ruang"];
        $this->kapasitas_kuliah = $post["kapasitas_kuliah"];
        $this->kapasitas_ujian = $post["kapasitas_ujian"];
        $this->layout = $post["layout"];
        $this->fasilitas = $post["fasilitas"];
        $this->db->insert($this->_table, $this);
    }

and here part of form view

<form action="<?php base_url('ruang/add') ?>" method="post" enctype="multipart/form-data" >

                    <div class="form-group">
                        <label for="ruang">Nama Ruang</label>
                            <input class="form-control <?php echo form_error('ruang') ? 'is-invalid':'' ?>"
                                type="text" name="ruang" placeholder="Masukkan nama ruangan" />
                        <div class="invalid-feedback">
                            <?php echo form_error('ruang') ?>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="kapasitas_kuliah">Kapasitas Kuliah</label>
                            <input class="form-control <?php echo form_error('kapasitas_kuliah') ? 'is-invalid':'' ?>"
                                type="number" name="kapasitas_kuliah" min="0" placeholder="Tentukan kapasitas kuliah" />
                        <div class="invalid-feedback">
                            <?php echo form_error('kapasitas_kuliah') ?>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="kapasitas_ujian">Kapasitas Kuliah</label>
                            <input class="form-control <?php echo form_error('kapasitas_ujian') ? 'is-invalid':'' ?>"
                                type="number" name="kapasitas_ujian" min="0" placeholder="Tentukan kapasitas ujian" />
                        <div class="invalid-feedback">
                            <?php echo form_error('kapasitas_ujian') ?>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="layout">Layout</label>
                            <input class="form-control" 
                            data-inputmask="'mask': ['99 x 99']" data-mask 
                                type="text" name="layout" placeholder="Tentukan layout ruangan" />
                    </div>

                    <div class="form-group">
                        <label for="fasilitas">Fasilitas Tersedia</label> <br>
                            <input type="checkbox" name="fasilitas[]" value="Proyektor"> Proyektor
                            <br>
                            <input type="checkbox" name="fasilitas[]" value="Papan Tulis"> Papan Tulis
                            <br>
                            <input type="checkbox" name="fasilitas[]" value="Jam Dinding"> Jam Dinding
                            <br>
                            <input type="checkbox" name="fasilitas[]" value="AC"> AC
                            <br>
                            <input type="checkbox" name="fasilitas[]" value="Kipas Angin"> Kipas Angin
                            <br>
                            <input type="checkbox" name="fasilitas[]" value="Tong Sampah"> Tong Sampah
                        <div class="invalid-feedback">
                            <?php echo form_error('fasilitas') ?>
                        </div>
                    </div>

                    <input class="btn btn-success" type="submit" name="btn" value="Save" />
                </form>

This really hinders my project, I hope someone can help

2
  • you mean, how to get all checked boxes and store it to database? Commented Sep 20, 2019 at 2:28
  • yes, that's what i mean and the results to the database are like this : ( Proyektor, Papan Tulis, Jam Dinding, Ac, Kipas Angin, Tong Sampah ) Commented Sep 20, 2019 at 11:03

4 Answers 4

1

You can use the following line too :

$fasilitas = implode(',', $this->input->post( 'fasilitas' , TRUE ) );

Sign up to request clarification or add additional context in comments.

Comments

0

If you can save fasilitas in your database as string. Then you can implode fasilitas array with comma separated as shown below:

$this->fasilitas = implode(',',$post["fasilitas"]);

it will stored in back-end side(Database) something like that.

Proyektor,Papan Tulis

I hope this will works for you.

Comments

0

You Can Use This to Get fasilitas as array :

$fasilitas = $this->input->post('fasilitas'); // Like array('AC','Proyektor','Kipas Angin');

Comments

0

In order for you to get all the checked boxes store in database, write this code.

$values = $post['fasilitas'];
$fasilitas = "";
 foreach($values as $val)
 {
    $fasilitas .= $val . ", ";
 }

Then store $fasilitas to db.

 $data = array(
               'fasilitas' => $fasilitas,
         );

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

Hope that helps :)

Comments

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.