PHP & MySQL - Generate invoice number from an integer from the database

Learning and sharing picture Learning and sharing · Nov 9, 2015 · Viewed 27.6k times · Source

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!

Answer

Learning and sharing picture Learning and sharing · Nov 9, 2015

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