我是PHP的新手,所以不确定我是否做正确的事,但是有人可以给我第二双眼睛吗?
基本上我想要的是,我们可以定义发票号码的初始启动,在此示例00001中 - 如果在最新条目的发票表中找不到这一点创建下一张发票时,将1添加到值中以显示。
我想它需要一个条件,以便如果实际表是空的,并且没有结果可以从00001
开始php到目前为止
html:
<input type="text" id="invoice_id" value="AMBMN<?php getInvoiceId(); ?>">
php:
// Initial invoice number
function getInvoiceId() {
define('INVOICE_INITIAL_VALUE', '00001');
// Connect to the database
$mysqli = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, DATABASE_NAME);
// output any connection error
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
$query = "SELECT invoice FROM invoices ORDER BY invoice DESC LIMIT 1";
// mysqli select query
$result = $mysqli->query($query);
//Invoice length
$invoice = $row['invoice'];
if(!$result){
echo INVOICE_INITIAL_VALUE;
} else {
// check if the initial invoice ID has already been used and if not start from that value else from the next value
if($invoice != INVOICE_INITIAL_VALUE) {
echo $invoice;
} else {
echo INVOICE_INITIAL_VALUE;
}
}
// Frees the memory associated with a result
$result->free();
// close connection
$mysqli->close();
}
当您获得查询的结果时,为发票编号创建变量,您可以执行此操作:
//Invoice number
define('INVOICE', '00001');
//Invoice length
$invoice_len = strlen(INVOICE);
//sprintf formatting string
$spf_format = '%0' . $invoice_len . 'd';
//Replace the 5 in %05d with however many characters there will be.
//In your case there are 5.
//It is now formatted with the 0s at the start.
$invoice = sprintf($spf_format, INVOICE);
echo $invoice;
当您说它从那时起破裂时,您的意思是完全停止吗?还是它也显示关闭标签?
当我在这里时,我不妨解释"%05D"在Sprintf内部做什么。
Sprintf命令用于更改变量的格式。开始时的%仅仅是定义的开始,它始终存在。0表示以下数字将控制变量的确切长度。因此,我们将其设置为5,因为有5个字符,并且使用此格式极不可能达到6个字符,但是如果这样做,您可以很容易地解决这个问题。因此,这就是%05-现在,末端的d基本上告诉PHP将变量视为整数,但将其显示为签名的小数(签名表示为负)。
。所有的工作都是告诉PHP将该变量视为5个字符的长整数,并将其显示为十进制,以便所有0持续存在。
查看此代码,首先我可以看到$result =mysqli->query()
$result
这里是一个mysqli对象,无法与常数进行比较。请先获取对象的值,然后进行比较。
i必须计算该行以工作的行,并且上述将结果设置为变量。请参阅下面:
// Connect to the database
$mysqli = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, DATABASE_NAME);
// output any connection error
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
$query = "SELECT invoice FROM invoices ORDER BY invoice DESC LIMIT 1";
// mysqli select query
$result = $mysqli->query($query);
// get number of returned results, if 0 action it below
$row_cnt = $result->num_rows;
//Invoice length
$invoice = $row['invoice'];
if($row_cnt == "0"){
echo INVOICE_INITIAL_VALUE;
} else {
// check if the initial invoice ID has already been used and if not start from that value else from the next value
if($invoice != INVOICE_INITIAL_VALUE) {
echo ++$invoice;
}
}
// Frees the memory associated with a result
$result->free();
// close connection
$mysqli->close();