How to handle dynamic multiple checkbox with update database in php

2019-08-19 Raj Thakur Download Code

Hello, friends welcome to the solutions touch tutorial now we solve the problem of how to handle and update multiple dynamic checkboxes in Codeigniter. It's very difficult to manage the multiple checkbox update at a time. Now we got a solution to this type of problem when the checkbox is updated then again update that time we got a problem to remove previously checked boxes next time. The solution is when we starting updating checkboxes firstly we set all these is ‘N’ then one by one selected checkbox is updated. At that time we need the primary key of that row to update accurately.

Create a database table:

The following SQL creates a check_box table in the MySQL database.

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";

CREATE TABLE `check_box` (
  `id` int(11) NOT NULL,
  `name` varchar(20) NOT NULL,
  `check` enum('Y','N') NOT NULL DEFAULT 'N'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `check_box` (`id`, `name`, `check`) VALUES
(1, 'php', 'N'),
(2, 'mysql', 'Y'),
(3, 'java', 'N'),
(4, 'python', 'N'),
(5, 'perl', 'Y');

ALTER TABLE `check_box`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `check_box`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
COMMIT;

Database Configuration (Application/config/database.php)

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => '',
	'database' => 'update_check_box',
	'dbdriver' => 'mysqli',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);

Dynamic Dependent Select Boxes View (Application/views/layout/index.php)

<!DOCTYPE html>
<html>
<head>
	<title>Update Check box</title>
	<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>Assets/bootstrap/css/bootstrap.min.css">
	<script type="text/javascript" src=""></script>
</head>
<body>
<div class="container" align="center">
	<h3>How to handle dynamic multiple checkbox with update database in codeigniter</h3>
	 <?php 
            if ($message=$this->session->flashdata('mgs')) {
               # code...
              $div_class=$this->session->flashdata('div-class');
            ?>
            <div class="alert <?php echo $div_class; ?>">
              <?php echo $message; ?>
            </div>
            <?php
             } 

             ?>
	<div class="row">
		<div class="col-lg-3"></div>
		<div class="col-lg-6">

		<?php
			echo form_open_multipart("Check_box/update/");

			?>		
			<table>
			<?php
				foreach ($data as $value) {
				
					if ($value->check=='N') {
						# code...
						?>		
						<tr>
						<td><?php echo $value->name; ?></td>
						<td><input type="checkbox" name="box[]" value="<?php echo $value->id; ?>"></td>
						</tr>		
						<?php
					}
					else{
							?>		
							<tr>
							<td><?php echo $value->name; ?></td>
							<td><input type="checkbox" name="box[]" checked value="<?php echo $value->id; ?>"></td>
							</tr>		
						<?php
						}
						}
					?>

			</table>
			<br>
			  <?php echo form_submit(['class'=>'btn btn-primary','value'=>'Update','type'=>'submit']); 
               echo form_close();
              ?>
		</div>
	</div>
</div>
</body>
</html>

Controller code (Application/Controllers/Check_box.php):

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Check_box extends CI_Controller {

	public function index()
	{
		$check_box=$this->data_model->all_details();
		$this->load->view('layout/index',['data'=>$check_box]);
	}


	public function update()
	{
		$input=$this->input->post('box');
		$this->data_model->check_box_n();
	
		foreach ($input as  $value) {
			# code...
			$this->data_model->check_box_y($value);
		}
		
		$this->session->set_flashdata('mgs','Update successfully.');
		$this->session->set_flashdata('div-class','alert-success');
		$check_box=$this->data_model->all_details();
		$this->load->view('layout/index',['data'=>$check_box]);

		# code...
	}
}

Database Model Code(Application/models/data_model.php):

<?php
class data_model extends CI_Model
{	
	// getting  all post
	public function all_details()
	{
		$detail=$this->db->select()
							->from('check_box')
							->get();
				return $detail->result();
	}
	public function check_box_n()
	{
				$this->db->set('check','N');
		return	$this->db->update('check_box'); 
	}
	public function check_box_y($id)
	{
			$this->db->set('check','Y');
			$this->db->where('id', $id);
	return	$this->db->update('check_box'); 
	}
}

About author

Card image cap
Raj Thakur

I’m a full-stack developer, with a strong programming experience on web base frameworks.!

-PHP developer

0 Comments