简体   繁体   中英

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. 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

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.

Then before inserting products create a new group then you will get a new groupID that will be unique.

Then using that groupID you can carry your option forward

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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