Jump to content

Updating multi dropdown values to database in Codeigniter


KSI

Recommended Posts

Currently I have a 2 dropdown inputs in my edit page, 1 a multi dropdown and the other is a single select dropdown. As of now when I enter the page, both of them show the values from my database.

Now the multi select shows a tick on all the options that are currently there in my database. So I want it so that whenever the user unchecks anything, it deletes the entry from my database and whenever they check something, it should insert that entry in the database.

Currently this is my code

Controller class:

function editteam($id,$return='')
    {
        if ($this->form_validation->run() == FALSE)
        {
            $main['return']=$return;
            $main['team']= $this->users_model->load_teams($id);  
            $main['members']=$this->users_model->get_members();    
            $main['page'] = 'crm/users/editteam';
         $this->load->view('crm/index', $main);
        } else {
            $maindata=array('status'=>$this->security->xss_clean($this->input->post('status')),
              'can_manage'=>$this->security->xss_clean($this->input->post('manage')),
              'role'=>$this->security->xss_clean($this->input->post('name')));
            $loginid=$this->users_model->update_role($maindata,$id);
            if($loginid){redirect('users/teams/'.$return);
            } else {    redirect('users/teams/'.$return);
            }
        }
    }

View class:

<?php
$membersArray = array();
foreach($team as $t){
$membersArray[] = $t['team_user'];
}
?>

    <div class="row card-box"><?php  echo form_open('users/editteam'.$team[0]['tid'].'/'.$return); ?>
      <div class="form-group col-sm-12">
        <label for="name">Team Name
        </label>
        <input id="name" name="name" type="text" class="form-control<?php echo $err; ?>" value="<?php  echo $team[0]['team_name']; ?>" />
      </div>
      <div class="form-group col-sm-12">
        <label for="manage">Managed By
          <?php } ?>
        </label>
        <select class="form-control multiselects <?php echo $err; ?>"  name="manage">
        
          <option value="0">None</option>
          <?php
              foreach($members as $r){
                  $selected = "";
                  if($team[0]['managed_by'] == $r['id']){
                    $selected = "selected";
                  }
                echo '<option '.$selected.' value="'.$r['id'].'">'.$r['display_name'].'</option>';
              }
          ?>
        </select>
      </div>
      <div>
        <label for="team">Team Members
        </label>
        <select multiple="multiple" class="form-control multiselects <?php echo $err; ?>" id="team" name="team[]">
          <?php
                foreach($members as $r){
                    $selected = "";
                    if(in_array( $r['id'] , $membersArray)){
                        $selected = "selected";
                    }
                    echo '<option '.$selected.' value="'.$r['id'].'">'.$r['display_name'].'</option>';
                }
          ?>
        </select>
      </div>
      <div class="form-group col-sm-12">
        <button type="submit" class="btn btn-info">Save</button>
      <?php echo form_close(); ?> </div>
  </div>

<script>
    $(document).ready(function(){
  $(".multiselects").multiselect({
      enableClickableOptGroups: true,
      enableFiltering: true,
      includeSelectAllOption:true, 
      buttonWidth: '100%', maxHeight:275, 
      buttonClass: 'form-control text-left bggrid',
      nonSelectedText: 'Select',
      enableCaseInsensitiveFiltering: true,
      nonSelectedText: 'Select',
      selectAllText: 'Select All',
      templates: { ul: '<ul class="multiselect-container dropdown-menu col-sm-12"></ul>'}
  });  $('#team').multiselect('select',[''],true);
});
</script>

Model class:

function get_members(){
        $this->db->select("display_name,id");
        $this->db->from("crm_clients_users");
        $query = $this->db->get();
        return $query->result_array();
    }
function load_teams($id)
    {
        $this->db->select("r.id AS tid , t.id as mid , r.,t.");
        $this->db->from("crm_clients_users_teams as r");
         $this->db->where("r.id",$id);
         $this->db->join("crm_clients_users_teams_members as t","r.id=t.team_id","left");
        $query = $this->db->get();
        return $query->result_array();
    }

crm_clients_users_teams_members holds the members of a particular team where I want to have the insert/delete of users whenever they are selected/unselected, and crm_clients_users_teams holds the title and other related stuff to the table.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.