mercredi 28 mars 2018

Getting "checked" on checkbox if exists in database pdo

I have to look for help over here because I don't have the patience anymore.

In the database I have a table: "colours" and there is a list of colours.

The second table is products, in "additional" row I have json data like:

{"size":["47"],"color":["yellow","green","orange"]}

I want to "check" boxes if they exist in an additional row, but the problem is when I have 2 boxes checked, input fields will be duplicated 2 times. If I have 3 boxes selected, it will be three times.

This is my code:

$sveBoje = colors::find_all();
$data = json_decode($produkt[0]->opis);
foreach($sveBoje as $key => $value){
    foreach($data->boja as $key2 => $value2){
        if($value->name == $value2){
            $checked = "checked ";
        }else{
            $checked = "";
        }
        echo "
            <div class='checkbox checkbox-success checkbox-inline checkbox-circle'>
                <input ".$checked." type='checkbox' id='".$value->name."' name='boja[]' value='".$value->name."'>
                <label for='".$value->name."'> 
                    <span style='background:".$value->color.";' class='colorBox'></span>
                </label>
            </div>
        ";
    }
}




Aucun commentaire:

Enregistrer un commentaire