dimanche 26 novembre 2017

How to make Dynamic checkbox

WHAT I WANT : I have 2 Different Tables and one have all categories ID and second one is places belong to these category now when i make edit option for the places and i want to show all category so if user want to add a new and also i want to show already chooses cat as checked checkbox.

WHAT I TRY : here is my code

<?php $sql1="SELECT * FROM `app_category` ";
         // var_dump($sql);
          $check1= mysqli_query($conn, $sql1);
          $resultcheck1= mysqli_fetch_array($check1,MYSQLI_BOTH);

                      foreach($check1 as $row){?>

                       <div class="checkbox-inline">
                           <span style="margin-left:10px"></span><input name="chk[]" value="<?php echo $row['cat_id'];?>" type="checkbox" id="checky"><span><?php echo $row['category_name'];?> </span><span style="margin-left:10px"></span>
                      </div>

                      <?php } ?>
                       <br>

                  </div>

here am fetching data from all categories and showing it on from now have second table having cat id as forign key and i want to check if the cat exist in second table it marked as a check : so i tried a logic :

<?php $sql11="SELECT cat_id FROM `app_placesmain` ";
         // var_dump($sql);
          $checkplace= mysqli_query($conn, $sql11);
          $resultcheck11= mysqli_fetch_array($checkplace,MYSQLI_BOTH);

i saved it to another array and now i want to check if the app_placemain have that category it marked as checked in checkbox i try array_diff ,if else etc etc but fail please help me out thanks in advance

i tried this now but still get all box checked here is my code :

<?php $sql1="SELECT * FROM `app_category` ";
         // var_dump($sql);
          $check1= mysqli_query($conn, $sql1);
          $resultcheck1= mysqli_fetch_array($check1,MYSQLI_BOTH);

 $sql1="SELECT * FROM `app_placesmain` ";
         // var_dump($sql);
          $check11= mysqli_query($conn, $sql1);
          $autocheck_array= mysqli_fetch_array($check11,MYSQLI_BOTH);


                      foreach($check1 as $row){$id=$row['cat_id'];?>

                       <div class="checkbox-inline">
                           <span style="margin-left:10px"></span><input name="chk[]" value="' 
. $id . '"' . (in_array($id, $autocheck_array) ? ' checked="checked"' : '')" type="checkbox" id="checky"><span><?php echo $row['category_name'];?> </span><span style="margin-left:10px"></span>
                      </div>

                      <?php } ?>
                       <br>




Aucun commentaire:

Enregistrer un commentaire