【问题标题】:How to generate auto increment id manually using codeigniter如何使用codeigniter手动生成自动增量ID
【发布时间】:2019-01-12 12:35:49
【问题描述】:

我是Codeigniter 的新手。如果不在数据库中使用自动增量,我想手动生成自动增量,例如 A00001, A00002, A00003 .......

如何使用 codeigniter 和 mysql 数据库生成这种类型的 ID?

【问题讨论】:

    标签: mysql codeigniter auto-increment


    【解决方案1】:

    实际上,自动自增更适合主键。但是如果你想生成它,你可以这样做

    function GenerateId() {
        $query = $this->db->select('ID')
                          ->from('table_name')
                          ->get();
        $row = $query->last_row();
        if($row){
            $idPostfix = (int)substr($row->ID,1)+1;
            $nextId = 'A'.STR_PAD((string)$idPostfix,5,"0",STR_PAD_LEFT);
        }
        else{$nextId = 'A00001';} // For the first time
        return $nextId;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-27
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 2013-04-11
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多