Use the str_pad function in PHP to generate product numbers in the form of numerical increments_PHP tutorial

WBOY
Release: 2016-07-21 16:13:26
Original
1385 people have browsed it

Solution: $str="QB".str_pad(($maxid[0]["max(id)"]+1),5,"0",STR_PAD_LEFT );

where $maxid[0 ]["max(id)"]+1) is to use the max function to find the value with the largest ID from the database. The ID is the primary key and will not be repeated.

str_pad() function pads a string to the specified length.

Grammar

str_pad(string,length,pad_string,pad_type)
Copy after login
Parameters Description
string Required. Specifies the string to be filled.
length Required. Specifies the length of the new string. If the value is less than the length of the original string, no operation is performed.
pad_string Optional. Specifies the string used for padding. The default is blank.
pad_type Optional. Specifies the side of the padding string. Possible values:
参数 描述
string 必需。规定要填充的字符串。
length 必需。规定新字符串的长度。如果该值小于原始字符串的长度,则不进行任何操作。
pad_string 可选。规定供填充使用的字符串。默认是空白。
pad_type 可选。规定填充字符串的那边。可能的值:

  • STR_PAD_BOTH – 填充到字符串的两头。如果不是偶数,则右侧获得额外的填充。
  • STR_PAD_LEFT – 填充到字符串的左侧。
  • STR_PAD_RIGHT – 填充到字符串的右侧。这是默认的。
  • STR_PAD_BOTH – Pad to both ends of the string. If it's not an even number, the right side gets extra padding.
  • STR_PAD_LEFT – Pad to the left side of the string.
  • STR_PAD_RIGHT – Pad to the right side of the string. This is the default.

http://www.bkjia.com/PHPjc/313501.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313501.htmlTechArticleSolution: $str="QB".str_pad(($maxid[0]["max(id) "]+1),5,"0",STR_PAD_LEFT ); where $maxid[0]["max(id)"]+1) is to use the max function to find the value with the largest ID from the database,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!