0

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 Answers2

0

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
...
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0

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);
}
manish
  • 423
  • 3
  • 9