I am trying to generate an id with fixed alphanumeric part and a varying 3 digit numeric part. That is, the id will look 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 is to generate this id's only once, that is repeating id has to be avoided. Also once the page is loaded, it should generate single id.This is like giving a bill number or something like that. I've created a function to generate this string upto a limit. But I need it to generate everytime the page is 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);
return $saved.$var;
}
}
echo generatebillno();
?>
Can anyone help me to do this. I am stuck here..
try this -
//fetch the max id (the last digit as number)
$sql = $mysqli->query('select max(cast(substring(demo_field, 21) as unsigned)) as digit,
demo_field from demo');
$res = $sql->fetch_assoc();
//check for 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);