doushi9856 2015-01-21 06:13
浏览 36
已采纳

使用codeigniter为多行使用相同的id

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++;
            }
  • 写回答

2条回答 默认 最新

  • douxuanling6523 2015-01-21 06:36
    关注

    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++;
                        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗