-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.sstatic.net/go4WY.jpg, Result is coming as expected. Only problem value from first foreach loop duplicating as growing. what's wrong in the code

3
  • 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.CommentedJul 21, 2022 at 10:41
  • 1
    Warning: You are wide open to SQL Injections and should use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. 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. Escaping is not enough!
    – Dharman
    CommentedJul 21, 2022 at 10:51
  • //this one coming duplicate this line is duplicating
    – Mithu
    CommentedJul 21, 2022 at 10:57

1 Answer 1

1
$namearray[]=$name; 

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

1
  • solved! i have to use this in second loop actually, thanks
    – Mithu
    CommentedJul 21, 2022 at 11:04

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.