Multiple foreach with different value to show checkbox selected

  checkbox, foreach, html, nested-for-loop, php

I am getting the checkbox list from the database and I have to check the list depending on the data get.

I have two arrays and below is the output. The first array is my all the list and second I am getting by the user selected.

Array(
    [0] => Array
        (
            [sp_id] => 1
            [specifications_name] => example 1
        )

    [1] => Array
        (
            [sp_id] => 2
            [specifications_name] => example 2
        )

    [2] => Array
        (
            [sp_id] => 3
            [specifications_name] => example 3
        )

    [3] => Array
        (
            [sp_id] => 4
            [specifications_name] => example 4
        )

    [4] => Array
        (
            [sp_id] => 5
            [specifications_name] => example 5
        )

    [5] => Array
        (
            [sp_id] => 6
            [specifications_name] => example 6
        )

)
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

I have to display all the lists from the database and I have checked the check box depending on the second array value getting the form database.

$specification=getgeneralSpecifications($pdo);// displaying all the list
$getgeneral = explode(",", $info['general_Specifications']); // coming from database
    
$allspecification=array_combine($specification,$getgeneral);
               
foreach ($allspecification as $key=> $row) {
    if ($row['sp_id']==) { // I have to compare with fetch data
        $checked="checked";
    }
    ?>
    <li><label><?php echo $row['specifications_name'];?></label>
        <div class="form-check">
            <input class="form-check-input custom-checkbox generalsinglecheck" type="checkbox" value="<?php echo $row['sp_id'];?>" name="general_specification[]"?> <?php echo $checked ?? '';?>>
        </div>
    </li>
                 
<?php } ?>

Source: Ask PHP

LEAVE A COMMENT