Insert multiple checkbox data using Codeigniter Share
In this tutorial, we are going to learn about how to create Insert multiple checkbox data using Codeigniter.
Step 1 :- Create tables in Database.
In this step, we are going to create two tables in database.
1) Students Table
CREATE TABLE `elevenstech`.`students` ( `id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR(200) NOT NULL , `email` VARCHAR(250) NOT NULL , `phone` VARCHAR(50) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
2) Added Student Table
CREATE TABLE `elevenstech`.`added_student` ( `id` INT NOT NULL AUTO_INCREMENT , `student_id` INT(11) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
Step 2 :- Insert Data in Student Table.
INSERT INTO `students` (`id`, `name`, `email`, `phone`) VALUES
(1, 'Kailash Singh', '[email protected]', '8376786284'),
(2, 'Amit Singh', '[email protected]', '8767327272'),
(3, 'Kailash Sharma', '[email protected]', '8376868378'),
(4, 'Siddarth Singh', '[email protected]', '9278297928'),
(5, 'Fari Singh', '[email protected]', '9287973287'),
(6, 'Avdhesh Singh', '[email protected]', '8926836988'),
(7, 'Lokesh Singh', '[email protected]', '9328797389'),
(8, 'Lakhan Verma', '[email protected]', '9289728939'),
(9, 'Manish Singh', '[email protected]', '8973298793'),
(10, 'Lovely Singh', '[email protected]', '9328987393'),
(11, 'Vikram Rana', '[email protected]', '9821797983'),
(12, 'Siddarth Verma', '[email protected]', '8732683233');
Step 3 :- Create Model in Models Folder (Student_model.php)
In this step we are fetching the data between the two tables Using join query.
<?php
class Student_model extends CI_Model{
public function student_list()
{
return $this->db->select('s.*, as.student_id')
->from('students as s')
->join('added_student as as','as.student_id = s.id','left')
->get()
->result();
}
}
?>
Step 4 :- Create Controller in Controllers Folder (Students.php)
In this step, we are going to pass the values from model to view.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Students extends CI_Controller
{
public function index()
{
$this->load->model('student_model');
$student_list = $this->student_model->student_list();
$this->load->view('students',['student_list'=>$student_list]);
}
}
?>
Step 5 :- Create view in views Folder (students.php)
In this step, we are creating a table to list all the data and also create a checkbox then we will put the condition in checkbox field to check the data exist in another table or not.
like this.
<input type="checkbox" name="student_id[]" value="<?php echo $student->id; ?>" <?php if($student->id == $student->student_id){echo'checked disabled';} ?>>
Here is the full code:-
In this code, we are also creating a form action to add multiple checkbox data.
<form action="<?php echo base_url(); ?>students/add_selected_student" method="post">
<button type="submit" name="submit" class="btn btn-danger">Add Selected</button>
<table class="table table-striped table-bordered" style="width:100%">
<thead>
<tr class="btn-primary">
<th></th>
<th>S.no.</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
<?php $i = 1; foreach ($student_list as $student) { ?>
<tr>
<td>
<input type="checkbox" name="student_id[]" value="<?php echo $student->id; ?>" <?php if($student->id == $student->student_id){echo'checked disabled';} ?>>
</td>
<td><?php echo $i; ?></td>
<td><?php echo $student->name; ?></td>
<td><?php echo $student->email; ?></td>
<td><?php echo $student->phone; ?></td>
</tr>
<?php $i++; } ?>
</tbody>
</table>
</form>
Step 6 :- Create Add Multiple Checkbox data query in Controller (Student_model.php)
public function add_selected_student()
{
$student_id = $this->input->post('student_id'); //here i am getting student id from the checkbox
for ($i=0; $i < sizeof($student_id); $i++)
{
$data = array('student_id' => $student_id[$i]);
$this->db->insert('added_student',$data);
}
$this->session->set_flashdata('msg',"Students details has been added successfully");
$this->session->set_flashdata('msg_class','alert-success');
return redirect('students');
}
Step 7 :- Create Success Message in View (students.php)
<?php if($error = $this->session->flashdata('msg')){ ?>
<div class="alert alert-success" id="msg">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Success!</strong> <?php echo $error; ?>
</div>
<?php } ?>
Hope this will help our developers.
Because we believe Mantra means CodingMantra





Click to Join









Comments 0