You can write a good helper function in PHP to use it where you want the account number to be returned in your application. The following helper function can simplify your process.
function invoice_num ($input, $pad_len = 7, $prefix = null) { if ($pad_len <= strlen($input)) trigger_error('<strong>$pad_len</strong> cannot be less than or equal to the length of <strong>$input</strong> to generate invoice number', E_USER_ERROR); if (is_string($prefix)) return sprintf("%s%s", $prefix, str_pad($input, $pad_len, "0", STR_PAD_LEFT)); return str_pad($input, $pad_len, "0", STR_PAD_LEFT); } // Returns input with 7 zeros padded on the left echo invoice_num(1); // Output: 0000001 // Returns input with 10 zeros padded echo invoice_num(1, 10); // Output: 0000000001 // Returns input with prefixed F- along with 7 zeros padded echo invoice_num(1, 7, "F-"); // Output: F-0000001 // Returns input with prefixed F- along with 10 zeros padded echo invoice_num(1, 10, "F-"); // Output: F-0000000001
Once you finish writing the helper function, you will not need to use CONCAT MySQL LPAD or CONCAT in your query each time to return an identifier with padding zeros or prefix zeros. If you have global access to an auxiliary function throughout the application, you need to call it only where you want to generate an account number.
source share