mardi 29 novembre 2016

Populating checkbox from server side data using 2 loops


I have a need to check the checkboxes which values are available in the database, with that i has to display additional options avaialable also.
I was trying, as i am using two loops it's repeating the same set of checkboxes and check differnt values in each instance.
I need to check the appropriate checkboxes in first loop itself. Is there any way to achieve this
The following was my output enter image description here
Following is the code i am using

$sid;//Retrived from DB
$iDLst=array();
$sql1 = "SELECT
`id1`
FROM `tbl1` 
where `tbl1_sid`='" . $sid . "'";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
    while ($row = $result1->fetch_assoc()) {
        $iDLst[]=$row['id1'];
    }
}
foreach ($iDLst as $id){
    $sql2 = "SELECT
    `id`,
    `nme`
    FROM `tbl2`;
    ";
$result2 = $conn->query($sql2);
    if ($result2->num_rows > 0) {
        while ($rowC = $result2->fetch_assoc()) {

            if (strpos($rowC['id'], $id) !== FALSE ) {
                echo ' <input value="' . $rowC['id'] . '" type="checkbox" name="upD[]" checked/>  <label>' . $rowC['nme'] . ' &nbsp;&nbsp;&nbsp;</label>';


            }  else {
                echo ' <input value="' . $rowC['id'] . '" type="checkbox" name="upD[]" />  <label>' . $rowC['nme'] . ' &nbsp;&nbsp;&nbsp;</label>';
            }


        }
    }
}

Note: I have changed to general code, There is no errors in code. I am getting the display. I need the solution regarding the logic part...




Aucun commentaire:

Enregistrer un commentaire