mardi 2 juin 2015

Looping checkbox array inside form

I've made a form that pulls data from database and then viewing it as table that can be edited by user. the data on database contains only 1 length integer, 1 and 0. if the data in DB is 1, then the checkbox checked, otherwise, it'll be not checked.

the code to query the data and viewing it looks like this :

<table class="tabel-lebar">
                <tr>
                    <th width="30px">ID</th>
                    <th width="80px">LEVELMN</th>
                    <th>NAMA</th>
                    <th>STATUS</th>
                    <th>ENTRY</th>
                    <th>VIEW</th>
                    <th>MODIFY</th>
                    <th>DELETE</th>
                    <th>APPROVAL</th>
                </tr>
                <?php
                    $lpd = new lvl_permission_dtl();
                    $perms = $lpd->getDetail($pdo, 'viewormod', $_GET['id']);
                    $colLength = 9;  //hard-coded column length nya (bukan total kolom nya, tapi kolom yang mau diambil aja

                    //print_r($perms[0][1]);
                    for($row=0; $row<count($perms); $row++){
                        echo "<tr>";
                        echo "<td width='30px'> <input readonly type='text' name='idnya[]' id='idnya[]' value='". $perms[$row][0] ."' /> </td>";
                        echo "<td width='80px'> <input disabled type='text' name='levelmn' id='levelmn' value='". $perms[$row][1] ."' /> </td>";
                        echo "<td> <input disabled type='text' name='namanya' id='namanya' value='". $perms[$row][2] ."' /> </td>";
                        echo "<td> <input type='checkbox' value='". $perms[$row][3] ."' name='statusnya[]' id='statusnya[]'". (($perms[$row][3]==1) ? 'checked' : '') ." /> </td>";
                        echo "<td> <input type='checkbox' value='". $perms[$row][4] ."'name='entrynya[]' id='entrynya[]'". (($perms[$row][4]==1) ? 'checked' : '') ."/> </td>";
                        echo "<td> <input type='checkbox' value='". $perms[$row][5] ."'name='viewnya[]' id='viewnya[]'". (($perms[$row][5]==1) ? 'checked' : '') ." /> </td>";
                        echo "<td> <input type='checkbox' value='". $perms[$row][6] ."'name='modifynya[]' id='modifynya[]'". (($perms[$row][6]==1) ? 'checked' : '') ." /> </td>";
                        echo "<td> <input type='checkbox' value='". $perms[$row][7] ."'name='deletenya[]' id='deletenya[]'". (($perms[$row][7]==1) ? 'checked' : '') ." /> </td>";
                        echo "<td> <input type='checkbox' value='". $perms[$row][8] ."'name='approvalnya[]' id='approvalnya[]'". (($perms[$row][8]==1) ? 'checked' : '') ." /> </td>";
                        echo "</tr>";
                    }   
                ?>
            </table>
            <div class="clear"></div>
            <input type="submit" name="btnSubmit" id="btnSubmit" value="Save" />

And when users change the data (by checkbox) in the form, the data should be updated in DB. But there's a problem, wherever i edit the checkboxes, the data submitted never changed. I knew that by printing the array that contains the form value..

the code i've made looks like this :

$details = array(); 
            $details_length = 13; //jumlah detail yang ada di form nya
            for($i=0; $i<$details_length; $i++){ //masukin objek Permision_dtl nya ke array $details
                $pdtl = new Permission_dtl();
                $pdtl->setID($_POST['idnya'][$i]);
                $pdtl->setStatus((!empty($_POST['statusnya'][$i])) ? 1 : 0);
                $pdtl->setEntry((!empty($_POST['entrynya'][$i])) ? 1 : 0);
                $pdtl->setView((!empty($_POST['viewnya'][$i])) ? 1 : 0);
                $pdtl->setModify((!empty($_POST['modifynya'][$i])) ? 1 : 0);
                $pdtl->setDelete((!empty($_POST['deletenya'][$i])) ? 1 : 0);
                $pdtl->setApproval((!empty($_POST['approvalnya'][$i])) ? 1 : 0);

                $details[$i] = $pdtl;
                //$statDetails = $pdtl->changeDetail($pdo, 'all', '');
            }

            print_r($details[0]);

Can anyone help me with this? :)




Aucun commentaire:

Enregistrer un commentaire