我正在尝试自动增加发票编号
i'm trying to increase the invoice number automatically
大家好,我正在尝试自动增加发票编号,问题是当涉及到 2019-10 自动 return 到 2019-01 她没有完成增加有人可以帮助我吗
<?php
$value2='';
//Query to fetch last inserted invoice number
$query = "SELECT numfacturation from facturation order by numfacturation DESC LIMIT 1";
$stmt = $con->query($query);
if(mysqli_num_rows($stmt) > 0) {
if ($row = mysqli_fetch_assoc($stmt)) {
$value2 = $row['numfacturation'];
$value2 = substr($value2, 6, 13);//separating numeric part
$value2 = $value2 + 1;//Incrementing numeric part
$value2 = "2019-" . sprintf('%02s', $value2);//concatenating incremented value
$value = $value2;
}
}
else {
$value2 = "2019-01";
$value = $value2;
}
?>
参见 substr() 的手册;
substr ( string $string , int $start [, int $length ] ) : string
Returns the portion of string specified by the start and length
parameters.
string
The input string. Must be one character or longer.
start
If start is non-negative, the returned string will start at
the start'th position in string, counting from zero. For
instance, in the string 'abcdef', the character at position 0 is
'a', the character at position 2 is 'c', and so forth.
注意“从零开始计数”部分。
考虑以下两个示例:
$value2 = '2019-XY';
$value2 = substr($value2, 6, 13);
var_dump($value2);
结果:string(1) "Y"
$value2 = '2019-XY';
$value2 = substr($value2, 5, 13);
var_dump($value2);
结果:string(2) "XY"
所以你需要统计从位置0
.
开始的字符
string: 2019-10
position: 0123456
如您所见,您的号码从位置 5
开始(而不是 6
)。
另请注意,您使用 13
作为长度。但是如果你的号码总是两位数,你应该使用:
$value2 = substr($value2, 5, 2);
大家好,我正在尝试自动增加发票编号,问题是当涉及到 2019-10 自动 return 到 2019-01 她没有完成增加有人可以帮助我吗
<?php
$value2='';
//Query to fetch last inserted invoice number
$query = "SELECT numfacturation from facturation order by numfacturation DESC LIMIT 1";
$stmt = $con->query($query);
if(mysqli_num_rows($stmt) > 0) {
if ($row = mysqli_fetch_assoc($stmt)) {
$value2 = $row['numfacturation'];
$value2 = substr($value2, 6, 13);//separating numeric part
$value2 = $value2 + 1;//Incrementing numeric part
$value2 = "2019-" . sprintf('%02s', $value2);//concatenating incremented value
$value = $value2;
}
}
else {
$value2 = "2019-01";
$value = $value2;
}
?>
参见 substr() 的手册;
substr ( string $string , int $start [, int $length ] ) : string
Returns the portion of string specified by the start and length parameters.
string
The input string. Must be one character or longer.
start
If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.
注意“从零开始计数”部分。
考虑以下两个示例:
$value2 = '2019-XY';
$value2 = substr($value2, 6, 13);
var_dump($value2);
结果:string(1) "Y"
$value2 = '2019-XY';
$value2 = substr($value2, 5, 13);
var_dump($value2);
结果:string(2) "XY"
所以你需要统计从位置0
.
string: 2019-10
position: 0123456
如您所见,您的号码从位置 5
开始(而不是 6
)。
另请注意,您使用 13
作为长度。但是如果你的号码总是两位数,你应该使用:
$value2 = substr($value2, 5, 2);