Generate an id with fixed alpha numeric string and variable integer part php -
i trying generate id fixed alphanumeric part , varying 3 digit numeric part. is, id like:
lab-ser-420316-1415-000 lab-ser-420316-1415-001 ... lab-ser-420316-1415-010 lab-ser-420316-1415-020 ... lab-ser-420316-1415-110 lab-ser-420316-1415-210 .....
my task generate id's once, repeating id has avoided. 1 time page loaded, should generate single id.this giving bill number or that. i've created function generate string upto limit. need generate everytime page loaded.
code:
<?php function generatebillno() { $saved="lab-ser-420316-1415-"; for($count = 0; $count <= 999; $count++) { $var= str_pad($count, 3, '0', str_pad_left); homecoming $saved.$var; } } echo generatebillno(); ?>
can help me this. stuck here..
try -
//fetch max id (the lastly digit number) $sql = $mysqli->query('select max(cast(substring(demo_field, 21) unsigned)) digit, demo_field demo'); $res = $sql->fetch_assoc(); //check empty valyues if (empty($res['demo_field'])) { $str = "lab-ser-420316-1415"; $res['digit'] = 0; } else { $str = substr($res['demo_field'], 0, 19); } $dig = $res['digit'] + 1; //add padding if needed $dig= str_pad($dig, 3, '0', str_pad_left); $newstr = $str.'-'.$dig; var_dump($newstr);
php
No comments:
Post a Comment