0


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 Output of the code
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...

Kuru
  • 738
  • 5
  • 18

2 Answers2

0

I think you can replace this:

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>';
            }

with this:

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

It's a ternary statement that says if strpos($rowC['id'], $id) evaluates true, 'checked ' will be in the enclosing echo statement, otherwise '' will be in the enclosing echo statement.

John Corry
  • 1,567
  • 12
  • 15
0

I have found the way after some research.
The way i am doing is only half part.
Following code will do the work addition to the provided code.

//Print the checkbox with checeked for the values in array
foreach ($iDLst as $id){
    $sql2 = "SELECT
    `id`,
    `nme`
    FROM `tbl2`;
    ";
$result2 = $conn->query($sql2);
    if ($result2->num_rows > 0) {
        while ($rowC = $result2->fetch_assoc()) {                                                                             
            echo ' <input value="' . $rowC['id'] . '" type="checkbox" name="upD[]" checked/>  <label>' . $rowC['nme'] . ' &nbsp;&nbsp;&nbsp;</label>';                                             
              }
        }
    }
    //Print the checkbox without checeked for the values Not in array
    $sql3 = "$sql2 = "SELECT
    `id`,
    `nme`
    FROM `tbl2` where id NOT IN (" . implode(',', array_map('intval', $iDLst)) . ");    ";

    $result3 = $conn->query($sql3);
    if ($result3->num_rows > 0) {
             while ($rowC = $result3->fetch_assoc()) {                                               

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

            }
    }

the following questions lead me the way to do this
MySQL PHP - SELECT WHERE id = array()? [duplicate]

mysql syntax on not equal many values

Community
  • 1
  • 1
Kuru
  • 738
  • 5
  • 18