duanlao6573 2017-03-01 06:59
浏览 47
已采纳

如何在codeigniter中的一列中插入多个字段数据

I have a form consist of multiple checkbox and input fields, I want to insert that data into a single column of a table, here is my form:

 <div id="container">
        <h1>property Detail</h1>
    <form action="" method="post">
    <table>
    <tr>
    <td> 
    Possesion
    <input type="checkbox" name="feature[]" value="possesion">
    </td>
    <td> 
    Possesion1
    <input type="checkbox" name="feature" value="possesion1">
    </td>
    <td> 
    Possesion2
    <input type="checkbox" name="feature" value="possesion2">
    </td>
    <td> 
    Possesion3
    <input type="checkbox" name="feature" value="possesion3">
    </td>
    <td> 
    Possesion4
    <input type="checkbox" name="feature" value="possesion4">
    </td>
    </td>
    </tr>

    <tr> 
    <td> <input type="submit" name="submit" value="submit"></td>
    </tr>
    </table>
    </form>

    </div>

here is my controller:

 function index(){
            $this->load->view('form');

            if($_POST){


            $data_feature  = array (
           'feature' => $_POST['feature']
             );

            $data['var']= $this->Mdata->p_detail($data_feature);
            }   

        }

and here is my model:

   function p_detail($data_feature){
             $this->db->insert('feature',$data_feature);
             return $this->db->insert_id();


}

I am getting only one feature value in my table, I want to get all the values of check boxes which user checked.

Regards

  • 写回答

5条回答 默认 最新

  • douxu0550 2017-03-01 07:22
    关注

    Best way to save it is json_encode to make the data in json string.There are many methods to save data like this:

    Firstly correct the html to make all same name fields as array like:

    <input type="checkbox" name="feature[]" value="possesion">
    

    Method 1:

    Make your data as json string to save:

    $this->db->insert('feature',json_encode($data));//data is an array need to insert()
    

    Method 2:

    Make your data as serialize array:

    $this->db->insert('feature',serialize($data));
    

    Method 3:

    Make your data array as string:

    $this->db->insert('feature',implode(",",$data));
    

    And if you want to row by row insertion then iterate posted values over loop like below:

    function index(){
    
                $this->load->view('form');
    
                if($_POST){
    
                    $data_feature  = $_POST['feature'];
    
                    $data = [];
                    foreach($data_feature as $f_key => $f_value){
    
                        $data[$f_key]['var']= $this->Mdata->p_detail($f_value);
    
                    }
    
                }   
    
            }
    

    And your modal use as;

    function p_detail($data_feature){
    
                 $this->db->insert('feature',$data_feature);//inserts into a single column 
                 return $this->db->insert_id();//returns last inserted id
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么