Show Multiple Checkbox Checked With Multiple Array Using Php

Rate this post

We are telling you by our best web post that how to show multiple checkboxes checked with multiple arrays using PHP Example.

Free Live Chat for Any Issue

Whenever you can checkbox checked with using the MySQL database then some users say it is the very difficulty not user friendly. but our article through you would easily step by step understand.

how to show multiple checkbox checked in php?

We will get to product information from the database and display different product information with the checkbox. how to display checked checkbox value in php? we selected product information inserts into another database. we need to inserted product information at edit time. because if already exists in the database and then the HTML checkbox should be checked in the edit form. below example in, the select_products array converts into the string with comma sign as well as explode function by string to convert into the array. if match array value after that the HTML checkbox will be checked or the HTML checkbox will be unchecked.

Read Also:  how to remove query string from url?

Also Read: PHP Multiple Checkbox Array Handling Tutorial Example

Download

index.php

<?php
       // select product value
	$select_products = array (
	  array("name"=>"Mobile","value"=>1),
	  array("name"=>"Laptop","value"=>2)
	);

       // product information
        $products_detail = array (
		array("name"=>"Mobile","value"=>1),
		array("name"=>"Laptop","value"=>2),
		array("name"=>"DVD","value"=>3),
		array("name"=>"Pendrive","value"=>4)
	);

	$string="";
	foreach ($select_products as  $key => $val) {
		$string .= ',' .$val['value'];
	}
	$string = substr($string,1);
	$string = explode(',',$string);

	foreach ($products_detail as $ld)
        {
		if(in_array($ld['value'],$string))
		{
		?>
		    <div class="form-group" style="margin-bottom: 0px;">
			    <input type="checkbox" name="products[]" checked value="<?php echo $ld['value']; ?>">
		        <label><?php echo $ld['name']; ?></label>
		     </div>
		<?php
		} else
		{
		?>
		   <div class="form-group" style="margin-bottom: 0px;">
		       <input type="checkbox" name="products[]" value="<?php echo $ld['value']; ?>">
		       <label><?php echo $ld['name']; ?></label>
		    </div>
		<?php
		}
	}
?>