jeudi 23 février 2017

Checkboxes with subelements? Keep data interity for database

How it is possible to create checkbox with subelements? The data is dynamic and there is in output from database more then 100 such boxes. I need to insert in DB only elements who are checked.

0 => 
    array (size=2)
      'type' => string 'triangle',
      'desc' => string 'my desc: 12_1'
1 => 
    array (size=2)
      'type' => string 'circle',
      'desc' => string 'my desc: 11_1'

The example of two HTML Boxes looks like:

<div id="box-1">
<label><input type="checkbox" name="elements[]" value="circle_form"/></label>
<input type="input" name="type" value="a type value" />
<input type="input" name="desc" value="a description" />
</div>

<div id="box-2">
<label><input type="checkbox" name="elements[]" value="triangle_form"/></label>
<input type="input" name="type" value="a type value" />
<input type="input" name="desc" value="a description" />
</div>

This is what I tried in PHP, but so I cannot keep data integrity:

$selected = [];
foreach ($request->elements as $index => $value) {
    $desc = $request->desc[$index];

    $my_element = [
        'type' => $value,
        'description' => $description
    ];

    $selected[$index] = $my_element;
}

foreach ($selected as $el) {
    // FRAMEWORK COMMAND TO INPUT ELEMENTS IN DB
}

My problem is how to sort out to get correct data into database? I'm also able to change HTML here.




Aucun commentaire:

Enregistrer un commentaire