Please how can I generate a unique id that increments when a new invoice is created. For example, I have an invoice with the id NR20200001 so the Next invoice created the Id should be NR20200002.
This is my code below, I can't seem to increment the unique id I generated, so I just generated it randomly, but I need to generate it incrementally
<?php
$qry = "SELECT * from requisitions order by req_id desc";
$result = mysqli_query($connection, $qry);
$row = mysqli_fetch_array($result);
$lastid = $row['req_id'];
if($lastid == ""){
$number = "NR".date("Y").date("s");
}
else{
$length=2;
$number = substr($lastid,4);
$number = intval($number);
$number = "NR".date("Y").date("s").substr(str_shuffle(str_repeat($x='0123456789',ceil($length/strlen($x)))),1,$length);
}
?>
Firstly, you should set req_id to primary key and auto increment on database
Secondly, add LIMIT 1 for get only the latest req_id:
$qry = "SELECT * from requisitions order by req_id desc LIMIT 1";
Next, change condition follow as below:
<?php
$qry = "SELECT * from requisitions order by req_id desc LIMIT 1";
$result = mysqli_query($connection, $qry);
$row = mysqli_fetch_array($result);
if($row['req_id'] == "" || $row['req_id'] == null)
{
$lastid = 0;
}
else
{
$lastid = $row['req_id'];
}
$nextid = sprintf("%04d", $lastid+1); //set to 4 digit
$number = "NR".date("Y").$nextid;
?>
Note: Do not delete record from table because it will make duplicate invoice number. You should add field flag for active/inactive.
Please check code for increment number
6)? $lastid: substr('00000000'. $lastid, -6); $getValue = "NR".date("Y").date("s").$getValue; }?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.