-2

I am trying to create a Nested Checkbox List in PHP. For this I have written Below Code with Multiple Nested foreach loop inside while

<?php
     $sql_concern="SELECT * FROM sister_concern  where Base_user='$User_id' And Status='Active'";
     $result_concern = mysqli_query($link,$sql_concern);
                                  
     if(mysqli_num_rows($result_concern) > 0){
         echo '<ul>';
         while($row = mysqli_fetch_array($result_concern)){
         $sisid=$row['Id'];
         $name=$row['Name'];
         $namearray[]=$name;

             foreach($namearray as $data){
             
            
             echo '<li style="list-style-type:none;"><input type="checkbox" name="sister_concern" class="chkMainConcern" id="chkMainConcern'.$sisid.'" value="'.$sisid.'" > '.$name.' </li>';
                                                 
             //fetch module Name for each concern
             $sql_module="SELECT * FROM Module where Status='Active'"; 
             $result_module = mysqli_query($link,$sql_module);
                 
                //this one coming duplicate                                
                 echo '<ul class="modulename'.$sisid.'" style="display:none;">';
                 while($rowmodule = mysqli_fetch_array($result_module)){
                 $modid=$rowmodule['Id'];
                 $modname=$rowmodule['Name'];
                 $modearray[]=$modname;

                      foreach($modearray as $val){
                         //module name 
                         echo '<li style="list-style-type:none;"><input type="checkbox" value="'.$modid.'"> '.$rowmodule['Name'].' </li>'; 
                                                        
                          //fetch module permission for each module
                          $sql_modhooks="SELECT * FROM Module_hooks where Status='Active' and ModuleID='$modid'";
                          $result_modhooks = mysqli_query($link,$sql_modhooks);
                          echo '<ul class="modulehooks'.$modid.'" style="display:none;">';
                          while($row_modhooks = mysqli_fetch_array($result_modhooks)){
                             echo '<li style="list-style-type:none;"><input type="checkbox"  value="'.$modid.'"> '.$row_modhooks['display_txt'].' </li>'; 
                          }
                          echo '</ul>';
                          }
                                                     
                     }
                     echo '</ul>';
                                             
                }
                                        
             }
             echo '</ul>';
    }else{
          echo 'No Concern Added Yet! Pls Add a Concer First';
    }
                                
?>

It is giving me Output like this https://i.stack.imgur.com/go4WY.jpg, Result is coming as expected. Only problem value from first foreach loop duplicating as growing. what's wrong in the code

Mithu
  • 665
  • 1
  • 8
  • 38
  • 1
    Which list is duplicating? Likely grouping results would resolve issue... or possibly just joining data. Don't use `select *` for every query, list the column(s) you want. This is open to SQL injections, parameterize queries and use prepared statements. – user3783243 Jul 21 '22 at 10:41
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Jul 21 '22 at 10:51
  • //this one coming duplicate this line is duplicating – Mithu Jul 21 '22 at 10:57

1 Answers1

1
$namearray[]=$name;

This appends to the existing array. Do unset($namearray) before this statement and it should remove your duplicates I feel.

user3783243
  • 5,368
  • 5
  • 22
  • 41