Consider array elements stored in table column as: 1,2,3,4,5
These corresponds to respective id of checkbox.if the id of the checkbox is equal to the one of the elements in(1,2,3,4,5) above,then it should be checked otherwise unchecked.

Can somebody write a script that can do so.

Thanks.

Try this

$tableArray = array(1,2,3,4,5);
$idArray = array(1,6,5);

foreach ($idArray as $myId) {
    $checked = (in_array($myId,$tableArray)) ? 'checked' : '';
    echo '<label>'.$myId.'</label><input type="checkbox" '.$checked.' value=""><br />';
}

you could create a function and use the function inside your input

Example:

function checked_array($id)
{
    $array_items = array('1','2','3','4','5');

    foreach($array_items as $item){
        if($item == $id){
            echo 'checked="checked";
        }
    }
}

<input type="checkbox" name="item[]" value="" <?php checked_array('2'); ?> />
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.