douque9815
2018-08-03 07:22
浏览 46
已采纳

在codeigniter中生成11位序列号

Here is my model function for generate serial numbers

public function getmax($field_name,$table_name){

        $this->db->select_max($field_name);
        $res1 = $this->db->get($table_name);
        if ($res1->num_rows() > 0) {
            $res2 = $res1->result_array();                  
        }
        $max = $res2[0][$field_name];
        if($max == NULL) return 1; else return $max+1;
    }   

Here is my controller Function

$CardNo = $this->general_model->getmax('card_number','digicardusers');

I want to set serial numbers starting from 00000000001.How to change the code to get 11 digit serial numbers.Any one please help

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douchongzhang9267 2018-08-03 07:34
    已采纳

    Presuming your just looking for the 0'padding, use sprintf()

    <?php
    for ($i = 1; $i < 25; $i++) {
        echo sprintf("%'.011d", $i).PHP_EOL;
    }
    

    https://3v4l.org/UanaJ

    00000000001
    00000000002
    00000000003
    00000000004
    00000000005
    00000000006
    00000000007
    00000000008
    00000000009
    00000000010
    00000000011
    ...
    
    点赞 评论
  • dongyan7172 2018-08-03 12:28

    you can use this function just pass param $digit

    function randomXDigit($digits = 11) {
        return str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT);
    }
    
    点赞 评论

相关推荐 更多相似问题