【问题标题】:php: how to get single value from a comma(,) separated arrayphp:如何从逗号(,)分隔的数组中获取单个值
【发布时间】:2013-05-08 06:17:47
【问题描述】:

我在我的项目中应用了标签功能,就像堆栈溢出一样。

当我从表单和控制器中提交多个标签时 我是这样理解的:

$this->input->post('tags_id') 所以我得到2,3,44,442 作为$this->input->post('tags_id') 的值。

我想要的是我希望将每个值一个一个地插入到我的标签表中。

如何从逗号分隔列表中获取每个值以插入到我的数据库中? 请帮帮我...

我正在执行以下操作以插入我的数据库

  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();
    }
}

【问题讨论】:

  • 检查explode 函数。将字符串拆分为数组。 str_getcsv 对带有一些特殊功能的逗号分隔值 (csv) 执行相同操作。

标签: php codeigniter


【解决方案1】:

你可以用php的explode函数轻松搞定

$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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多