KSI Posted November 17, 2021 Share Posted November 17, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/314243-updating-multi-dropdown-values-to-database-in-codeigniter/ Share on other sites More sharing options...
ginerjm Posted November 17, 2021 Share Posted November 17, 2021 So you have written all of this code. Good job. Where is this part you are asking about? The part that does the db updates? Most of is is quite complex so I assume that you are certainly capable of writing a simple db update process. Quote Link to comment https://forums.phpfreaks.com/topic/314243-updating-multi-dropdown-values-to-database-in-codeigniter/#findComment-1592197 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.