Jump to content
Kirasiris

How to use select2 in CodeIgniter and how to implode the values inserted on it.

Recommended Posts

Hello,

so I've been trying to use Select 2 in the AdminLTE template with a function in javascript to add dynamic data.

 

Capture3.jpg

 

 

Here is my view in which I'm using a form_input with a class of select2.:

            <div class="form-group">
                <?php echo form_label('Skills', 'skills'); ?>
                <div class="input-group date"><div class="input-group-addon"><i class="fa fa-star" aria-hidden="true"></i></div>
                <?php
                    $data = array(
                        'name'          => 'skills',
                        'id'            => 'skills',
                        'class'         => 'form-control select2',
						'style'			=> 'width:100%',
                        'value'         => $item->skills
                    );
                ?>
                
                <?php echo form_input($data); ?>
                </div>
            </div>

and here is the script which should allow me to add the skills:

$(".select2").select2({
    tags: true,
    tokenSeparators: [',', ' ']
})

Is it supposed to set the tags equal to TRUE allow me to add data? Because what I know the input when adding a new content should turn into a blue color, right?. It is not doing it.

 

 

SECOND PART

Now can somebody help me in how to correctly implode(separate by comma) the skills into my database?. I actually though that in was by just doing something like:

$user_skill = implode(' , ' , $this->input->post('skills'))

and then passing the data to the database users with:

'skills' => $user_skills,

but it is not working and I really don't know why(it's sending nothing to the database). It does not update my users.id; here is a pic:

Capture.jpg

    public function edit($id){
		
        // Check Login
        if (!$this->session->userdata('is_member')) {
            redirect('dashboard/login');
        }
		
		// Field Rules
        $this->form_validation->set_rules('first_name', 'First Name', 'trim|required|min_length[2]');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|min_length[2]');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[7]|valid_email');

        if ($this->form_validation->run() == FALSE) {
            // Get Current Subject
            $data['item'] = $this->User_model->get($id);
            //Load View Into Template
            $this->template->load('public', 'default', 'users/edit', $data);
        } else {
            $slug = str_replace(' ', '-', $this->input->post('username'));
            $slug = strtolower($slug);
			
			$user_skills = implode(',', $this->input->post('skills'));
            // Create User Data Array
            $data = array(
                'first_name' => $this->input->post('first_name'),
                'last_name'  => $this->input->post('last_name'),
                'email'      => $this->input->post('email'),
                'username'   => $this->input->post('username'),
				'slug'		 => $slug,
				'avatar_img' => $this->input->post('avatar_img'),
				'cover_img' => $this->input->post('cover_img'),
				'genre' => $this->input->post('genre'),
				'company' => $this->input->post('company'),
				'phone' => $this->input->post('phone'),
				'address' => $this->input->post('address'),
				'occupation' => $this->input->post('occupation'),
				'biography' => $this->input->post('biography'),
				'website' => $this->input->post('website'),
				'birthdate' => $this->input->post('birthdate'),
				'interested_in' => $this->input->post('interested_in'),
				'college' => $this->input->post('college'),
				'highschool' => $this->input->post('highschool'),
				'skills' => $user_skills,
            );

            // Update User
            $this->User_model->update($id, $data);

            // Activity Array
            $data = array(
                'resource_id' => $this->db->insert_id(),
                'type'        => 'user',
                'action'      => 'updated',
                'user_id'     => $this->session->userdata('user_id'),
                'message'     => '(' . $data["username"] . ') Updated his/her information'
            );

            // Add Activity  
            $this->Activity_model->add($data);
			
			// User Skills Array
			$data = array(
				'id' => $this->db->insert_id(),
				'skills' => $user_skills,
			);
			
			// Add User Skill
			$this->Skills_model->add($data);
					
            //Create Message
            $this->session->set_flashdata('success', "You're account has been updated");

            //Redirect to Users
            redirect('dashboard/profile');
        }
    }

Fetching the skills in the view would be something like this or do I need to do a different approach. 

<?php echo $this->session->skills; ?>

THIRD PART

 

Also I would like to know if somebody knows how to add skills to a database table depending on the skills added by the user on his account?

 

As you can see in the controller I created something like:

 

// User Skills Array
$data = array(
'id' => $this->db->insert_id(),
'skills' => $user_skills,
);


// Add User Skill
$this->Skills_model->add($data);

How can I make sure that all the data inside the variable $user_skill fits into my user_skills table. I mean the table should be increasing its rows according to the number of skills added by the users.

 

Capture2.jpg

 

I hope somebody can helps and understand what I mean. Thanks.

Share this post


Link to post
Share on other sites

You are defining a variable "$user_skill" then using it later as "$user_skills"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.