user2180861
user2180861

Reputation: 37

php: how to get single value from a comma(,) separated array

i have applied tags functionality in my project just like stack overflow.

When i subit multiple tags from my form, and in my controller i am getting it like this:

$this->input->post('tags_id') so i am getting 2,3,44,442 as the value for $this->input->post('tags_id') altogether.

What i want is i want the each value to be inserted into my tags table one by one.

How can i get the each value from from the comma separated list to be inserted in my db? Please help me...

I am doing the following to insert into my db

  function entry_insert_instdetails()
{ 
   $this->load->database();     
   $this->db->trans_begin();  
   $data=array('instrunction'=> $this->input->post('tags_id'),   
   'creater_id'=>$this->session->userdata('userid'), 
    ); 
    $this->db->insert('instructions',$data); 

  if ($this->db->trans_status() === FALSE)
    {
        $this->db->trans_rollback();
    }  
  else 
    {
        $this->db->trans_commit();
    }
}

Upvotes: 0

Views: 3405

Answers (1)

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

You can do it easily with explode function of php

$data   =   array('instrunction'=> $this->input->post('tags_id');

$ids    =   explode(',',$data);

unset($data);

foreach($ids as $id)
{
    $data['tag_id'] =   $id;    // tag_id is table column
    $this->db->insert('instructions',$data);
    unset($data);
}

Upvotes: 2

Related Questions