简体   繁体   English

使用Codeigniter对多个行使用相同的ID

[英]Using same id for multiple rows using codeigniter

I have a table with three fields 我有一张桌子,上面有三个字段

id (primary key / auto incremented)
product_name
group_id

my problem is when i insert multiple rows through form the whole group of rows should get same groupId & it should be incremented by 1 at the time of submission as there can be many users submitting the form at the same time. 我的问题是,当我通过表单插入多行时,整个行组应具有相同的groupId,并且在提交时应将其递增1,因为可能有许多用户同时提交表单。 I dont know how to do it. 我不知道该怎么做。 Please help. 请帮忙。

my model 我的模特

function get_last_group_id() {
            $this->db->select('group_id');
            $this->db->from('mytable');
            $this->db->order_by('group_id', 'DESC');
            $this->db->limit('1');
            $query = $this->db->get();
            return $query->result();
          }

          function save_rows($ids,$product_names,$group_ids){
            $this->db->trans_begin();
            $ndx=0;
            foreach($ids as $id){

            $data = array(
            'id' => $id,
            'product_name' => $product_names[$ndx],
            'group_id' =>$group_ids[$ndx],
            $this->db->insert("product_details",$data);

            $this->db->update($this->table);
            $ndx++;
            }

There is no need to make seperate function for getting group id and set id field also if that is auto incre 即使是自动递增的,也无需为获取组ID和设置ID字段设置单独的功能

function save_rows($ids,$product_names){
                    $this->db->trans_begin();
                    $ndx=0;
        $group_id = $this->db->select("MAX(group_id) as group_id")
        ->from("mytable")
        ->get()->row_array();

                    foreach($ids as $id){

                    $data = array(
                    'product_name' => $product_names[$ndx],
                    'group_id' =>$group_id['group_id']+1,
    );
                    $this->db->insert("product_details",$data);

                    $ndx++;
                    }

You can create a new table say 'groups' fields: id(Auto Increment) and group_name (varchar) generate group name randomly. 您可以创建一个新表,例如“ groups”字段:id(Auto Increment)和group_name(varchar)随机生成组名。

Then before inserting products create a new group then you will get a new groupID that will be unique. 然后,在插入产品之前,创建一个新的组,然后您将获得一个唯一的新组ID。

Then using that groupID you can carry your option forward 然后使用该groupID可以继续进行选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM