In the PHP development project, the customer requires that the product number be generated automatically when the product number is used, in the format of "QB00001". the solution of increasing the number is $ str = "QB ". str_pad ($ maxid [0] ["max (id)"] + 1), 5, "0", STR_PAD_LEFT );
$ Maxid [0] ["max (id)"] + 1) is used to find the largest value of ID from the database using the max function. ID is the primary key and will not be repeated.
The str_pad () function fills the string with the specified length.
Syntax
Str_pad (string, length, pad_string, pad_type)
Parameters |
Description |
String |
Required. Specifies the string to be filled. |
Length |
Required. Specify the length of the new string. If the value is smaller than the length of the original string, no operation is performed. |
Pad_string |
Optional. The specified string for filling. The default value is blank. |
Pad_type |
Optional. Specify the other side of the string to be filled. Possible values:
- STR_PAD_BOTH-fill in the two ends of the string. If it is not an even number, the right side will receive additional filling.
- STR_PAD_LEFT-fill in the left side of the string.
- STR_PAD_RIGHT-fill in the string to the right. This is the default one.
|