Failed to auto increment when using two latter in start of bill no

Hi to All! Respected member of this forum Miss Lara help me alot to solve the issue but I am facing one issue with that code,When Instead of B, if i used two latter (for example “BA” instead of only B) then its stop incrementing the number for next entry in the form.

<?php // Get the current month and year as two-digit strings
 $month = date("m"); 
$year = date("y");
 // e.g. 23 
// Get the last bill number from the database
 $query = "SELECT patientno FROM iap2 ORDER BY patientno DESC";
 $result = mysqli_query($conn,$query); 
// Use mysqli_fetch_assoc() to get an associative array of the fetched row
 $row = mysqli_fetch_assoc($result); 
// Use $row['patientno'] to get the last bill number
 $lastid = $row['patientno'];
 // Check if the last bill number is empty or has a different month or year
 if(empty($lastid) || substr($lastid, 2, 2) != $month || substr($lastid, 4, 2) != $year) {
 // Start a new sequence with 0001 
$number = "Ls-$month$year-0001"; 
 // Increment the last four digits by one
 $idd = substr($lastid, 8);
 // e.g. 0001
 $id = str_pad($idd + 1, 4, 0, STR_PAD_LEFT);
 // e.g. 0002 
$number = "Ls-$month$year-$id"; 

If you change the format from “A-$month$year-0001” to “AB-$month$year-0001” you’ll have to adjust all substrings in your code to handle this new format.

1 Like

i did change string setting as well, when there is only “B” then it was
$idd = substr($lastid, 7);
When I used “AB” then I adjusted it to
$idd = substr($lastid, 8);
but its still not incrementing

Did you adjust this line as well?

 if(empty($lastid) || substr($lastid, 2, 2) != $month || substr($lastid, 4, 2) != $year) {

The month and year will be in different places too.

1 Like

This was addressed in this thread Auto generate bill no in php mysql

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.