8

I need to generate an invoice number from an integer of a table with an auto incrementing ID of the database where the user purchases saved.

Example of the table invoice database:

enter image description here

The invoice number format floor do one of two ways.

Example 1: of the number of invoices without prefix:

0000001 | 0000002 | 0000003 | 0000004 | 0000005

Example 2: the number of invoices with prefixes:

F-0000001 | F-0000002 | F-0000003 | F-0000004 | F-0000005

Question:

1) ¿What is the best way to do this, you can do directly from MySQL or PHP?

2) ¿What is the most appropriate format Example 1 or Example 2?

I appreciate your support as always!

2
  • I think you want to learn about LPAD() and CONCAT(). Commented Nov 9, 2015 at 4:11
  • I used CONCAT () but I have not used LPAD (), is that CONCAT () could concatenate the ID with zeros I need, but not if this function can have the values and not extend zeros as they increased the ID number, please can provide an example? Commented Nov 9, 2015 at 4:17

5 Answers 5

12

Thanks to Gordon Linoff, I could get a way to solve this.

I will share an example, perhaps someone may be interested.

SQL - Invoice without prefix: SELECT id, LPAD(id,7,'0') FROM invoice WHERE id = 1;

Result: 0000001

SQL - Invoice with prefix: SELECT id, CONCAT( 'F-', LPAD(id,7,'0') ) FROM invoice;

Result: F-0000001

1
  • Just to mention that we still have to use the php part to generate the invoice if there are no records in the table Commented Nov 28, 2018 at 9:22
11

You can write a good helper function in PHP to use it wherever you want in your application to return an invoice number. 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 are done writing the helper function, you don't need to use LPAD or CONCAT MySQL functions every time in your query to return ID with padding zeros or zeros with prefix. If you have global access to the helper function in the entire application, you only need to invoke it wherever you want to generate an invoice number.

1
  • Glad it seems really helpful to you!
    – Wolverine
    Commented Jul 1, 2018 at 17:44
6

1 - 0000001 | 0000002 | 0000003 | 0000004 | 0000005

$dbValue = 1; echo $dbValue = str_pad($dbValue, 7, "0", STR_PAD_LEFT); // it will give 0000001;

2 - F-0000001 | F-0000002 | F-0000003 | F-0000004 | F-0000005

$dbValue = 1; echo $dbValue = "F-".str_pad($dbValue, 7, "0", STR_PAD_LEFT); // it will produce F-0000001;

3
  • I could do it straight with MySQL, but it's good to know that in PHP, thank you very much. Commented Nov 9, 2015 at 4:35
  • better practice if you using sql statement to prefix + padding string.
    – ZenithS
    Commented Nov 9, 2015 at 4:38
  • this would start eliminating the 0 from the padded string as the $dbValue start increasing to 2, 3, 4 digits and so on, should it keep the seven 0 in the start and then append the incremented $dbValue like 0000000123 your answer will have 0000123 Commented Nov 27, 2018 at 17:23
5

Fetch last ID from database and store it in a PHP variable.

For example, if last record is 100, then increment it by 1.

$last = 100; // This is fetched from database
$last++;
$invoice_number = sprintf('%07d', $last);

Finally, the answer for second question is,

$number = "F-". $number;
1
2

Ans 1):

You can do this with PHP(directly by concat or use str-pad ) as well as with MySQL( LPAD ) also

But as per my view you should do this by PHP, so that you can change it according to your requirements e.g. extend zeroes as per number of id's in DB.So that not to change SQL queries and make it heavy.

Ans 2): You can use both formats but if you want to be more specific about particular user or any thing else, then use second format.

I think second format can give you more information about data

Not the answer you're looking for? Browse other questions tagged or ask your own question.