Reputation: 81
In my php code, while inserting new record, invoice number should be 0001. Here I want to add characters/a word(for example 'optical') and it should be like optical0001
. How can I add this before the number?
$query = "select * from cust_report_invoices order by invoiceID desc limit 1";
$result = $link->query($query);
$row = $result->fetch_assoc();
$invoiceNo = $row['invoiceNo'];
$prefix = 'Baopt_';
$getinvoiceNo = $prefix . str_pad($invoiceNo + 1, 4, 0, STR_PAD_LEFT);
$sql = "INSERT INTO cust_report_invoices (invoiceNo,invoiceDate)
VALUES ('$getinvoiceNo', '$getinvoiceDate' )";
Upvotes: 0
Views: 70
Reputation: 1711
When working in PHP you can use the .
as concatenation-operator. I see that you use the function str_pad
that returns as string.
So you can just concat the prefix with the invoice number like this.
$prefix = 'optical';
$invoice = $prefix . str_pad($invoiceNo + 1, 4, 0, STR_PAD_LEFT);
$prefix
can be anything you want.
When invoice = 0001
this example will return optical0001
Upvotes: 1
Reputation: 11642
str_pad
return string (doc). So just concat with .
:
$prefix = 'optical';
$invoice = $prefix . str_pad($invoiceNo + 1, 4, 0, STR_PAD_LEFT);
Edit
When adding new element you need to cut the prefix out - you can use this:
$prefix = 'Baopt_';
$invoiceNo = $row['invoiceNo'];
if (substr($invoiceNo, 0, strlen($prefix)) == $prefix) // if already has prefix
$invoiceNo = substr($invoiceNo, strlen($prefix)); // remove it
$getinvoiceNo = $prefix . str_pad($invoiceNo + 1, 4, 0, STR_PAD_LEFT);
Upvotes: 2
Reputation: 484
Not sure if I understand correctly, you want to prepend "optical" to $getinvoiceNo?
If yes then it is simple,
invoiceId = "optical$getinvoiceNo"
Upvotes: 0
Reputation: 184
Just "Opticals".$getinvoiceNo
Dot is concatenating strings.
Upvotes: 0