samedi 13 janvier 2018

PHP - Loop checkboxes and save it to the database

I am having a challenge in PHP and HTML check boxes. Would you help me with this?

Here is my scenario:

I have a User Role module where you will create a user role and then assign what modules that role can access using check boxes. I will attach a screenshot.

What I want to do is to loop through that check box array, and then save each checkbox with a check on it.

Getting all the checked check boxes is fine. I have found a code for that.

Now, I checked a check box for example Delivery module, and then save it, of course it will go to the database. What if I edited the user role, and then unchecked the Delivery module, and saved it, should I delete that row? Because what I understand each checkbox will be saved as a row in the database.

That is the part that I can't imagine how to implement. Would you help me how is the design for that?

Hope I explained myself well. Please let me know if there are unclear parts. Thank you any suggestion, I appreciate it.

Here are some details Hope it would help:

I am using CodeIgniter and MS SQL Server as database.

Here is my code:

          <?php if ($this->session->userdata('portalModules')):
            $portalModules = $this->session->userdata('portalModules');
            $prevGroup = NULL;
            $firstGroup = TRUE;
          ?>
          <?php foreach ($portalModules as $moduleGroup): ?>
            <?php if ($moduleGroup['moduleGroup'] !== $prevGroup): ?>
              <?php if (!$firstGroup): ?>
                </div>
              </div>
            <?php else: ?>
              <?php $firstGroup = FALSE; ?>
              <?php endif ?>

              <?php $prevGroup = $moduleGroup['moduleGroup']; ?>
              <div>
                <div class="form-group"><input type="checkbox" name=""><?php echo $moduleGroup['moduleGroup']; ?></div>
                <div>
                  <?php foreach ($portalModules as $menu): ?>
                    <?php if ($menu['moduleGroup'] == $moduleGroup['moduleGroup']): ?>
                        <div class="form-group">
                        <p style="margin-left: 20px;"><input type="checkbox" name=""><?php echo $menu['moduleName']; ?></p>
                                
                        </div>
                    <?php endif ?>
                  <?php endforeach ?>
            <?php endif ?>
            
          <?php endforeach ?>

Here is the screenshot of check boxes.




Aucun commentaire:

Enregistrer un commentaire