php 使用自定义代码生成多行的代码
php code to generate multiple rows with custom code
前-
如果用户在文本框中输入
bookno - 101 totalbook - 2
receiptno - 500 totalrec - 2
优惠券 - 700 总优惠券 - 2
然后输出如下 table。
在这个table中,优惠券号总是unique.and收据没有来两次,因为每张收据都有两张优惠券。
前-
书票券
101 500 - 700
101 500 - 701
101 - 501 - 702
101 - 501 - 703
102 - 502 - 704
102 - 502 - 705
102 - 503 - 706
102 - 503 - 707
现在第二次输入
书本代码 = 800
book_no2 = 802
收据代码 = 1600
Temp_receipt = 1602
优惠券代码 = 1800
Temp_coupon = 1802
然后生成如下输出
书票券
801 -1601 1801
801 -1601 -1802
801 -1602 -1803
801 -1602 -1804
802 -1603 -1805
802 -1603 -1806
802 -1604 -1807
802 -1604 -1808
我尝试了下面的代码但无法正常工作。
<?php
if(isset($_POST['save']))
{
$bookcode = $_POST['bookcode'];
$book_no2 = $_POST['book_no2'];
$receiptcode = $_POST['receiptcode'];
$receipt_no = $_POST['receipt_no'];
$couponcode = $_POST['couponcode'];
$coupon = $_POST['coupon'];
$Temp_receipt = $receiptcode + $receipt_no;
$Temp_coupon = $couponcode + $coupon;
for($row1=$bookcode+1;$row1<=$bookcode+$book_no2;$row1++)
{
for($row=$receiptcode+1;$row<=$Temp_receipt;$row++)
{
$query = $database->getRow("SELECT MAX(receipt_no) AS max1 FROM scheme_master;");
if($query['max1']=='')
{
$largestNumber = $receiptcode;
$top = $largestNumber + 1;
}
else
{
$largestNumber = $query['max1'];
$top = $largestNumber + 1;
}
$Pric = "";
$loopCount = 0;
for($row2=$couponcode+1;$row2<=$Temp_coupon;$row2++)
{
$query = $database->getRow("SELECT MAX(coupon) AS max2 FROM scheme_master;");
if($query['max2']=='')
{
$largestcoupon = $couponcode;
$coup = $largestcoupon + 1;
}
else
{
$largestcoupon = $query['max2'];
$coup = $largestcoupon + 1;
}
$value = $loopCount++ + 1;
$code = '- mths';
$Pric=$value.$code;
$insertrow = $database->insertRow("INSERT INTO scheme_master (book_no2,receipt_no,coupon)
VALUES (:book_no2,:receipt_no,:coupon)",
array(':receipt_no'=>$top,':book_no2'=>$row1,':coupon'=>$coup));
}
}
}
$_SESSION['message'] = "Books Created Successfully";
}
?>
我得到如下输出 table...
书票券
101 - 501 -701
101 - 502 -702
102 - 501 -701
102 - 502 -702
每次使用循环时,它都会重置回输入的值(例如,coupon_no)
您应该使用 bookno
、couponno
和 receiptno
的值作为起点,并使用 totalbook
、totalcoup
和 [=16] 的值=] 作为循环条件。边走边增加数字:
<?php
$bookno= $_POST['bookcode'];
$totalbook= $_POST['book_no2'];
$receiptno = $_POST['receiptcode'];
$totalrec= $_POST['receipt_no'];
$couponno= $_POST['couponcode'];
$totalcoup= $_POST['coupon'];
for ($book_counter=1; $book_counter<=$totalbook; $book_counter++)
{
for($rec_counter=1; $rec_counter<=$totalrec; $rec_counter++)
{
for($coup_counter=1; $coup_counter<=$totalcoup; $coup_counter++)
{
$insertrow = $database->insertRow(
"INSERT INTO scheme_master (book_no2,receipt_no,coupon) VALUES (:book_no2,:receipt_no,:coupon)",
array(':receipt_no'=>$receiptno,':book_no2'=>$bookno,':coupon'=>$couponno));
$couponno++;
}
$receiptno++;
}
$bookno++;
}
?>
注意这还没有经过测试,但它应该会让你走上正确的道路
编辑后的代码(带有关于未使用变量的注释)
<?php
if (isset($_POST['save']))
{
/*
* No checking for validity of values
*/
$bookcode = $_POST['bookcode'];
$book_no2 = $_POST['book_no2'];
$receiptcode = $_POST['receiptcode'];
$receipt_no = $_POST['receipt_no'];
$couponcode = $_POST['couponcode'];
$coupon = $_POST['coupon'];
$Temp_receipt = $receiptcode + $receipt_no;
$Temp_coupon = $couponcode + $coupon;
for ($row1 = $bookcode + 1; $row1 <= $bookcode + $book_no2; $row1++) // what happens if $bookcode or $book_no2 are negative? Why start at +1 instead of what user entered?
{
for ($row = $receiptcode + 1; $row <= $Temp_receipt; $row++) // what happens if $receiptcode or $receipt_no are negative? Why start at +1 instead of what user entered?
{
/*
* Each run of this query will slow down this whole code
*/
$query1 = $database -> getRow("SELECT if (receipt_no is null, 1,MAX(receipt_no)+1) AS max1 FROM scheme_master;");
$top = $query1['max1'];
/*
* Initialised but $Pric is never used apart from an assignment
*/
$Pric = "";
$loopCount = 0; // initialised and updated but not used anywhere in this code
for ($row2 = $couponcode + 1; $row2 <= $Temp_coupon; $row2++) // what happens if $couponcode or $coupon are negative? Why start at +1 instead of what user entered
{
/*
* Each run of this query will slow down this whole code
*/
$query2 = $database -> getRow("SELECT if (coupon is null, 1, MAX(coupon)+1) AS max2 FROM scheme_master;");
$coup = $query2['max2'];
/*
* $value is never used
*/
$value = $loopCount+=2; // the same as $value=$loopCount++ +1; No idea why it's here as it is not used at all in this code
/*
* These values are never used
*/
$code = '- mths'; //unused. As it never changes, should be initialised earlier
$Pric = $value.$code; //unused
$insertrow = $database -> insertRow("INSERT INTO scheme_master (book_no2,receipt_no,coupon) VALUES (:book_no2,:receipt_no,:coupon)", array(':receipt_no' => $top, ':book_no2' => $row1, ':coupon' => $coup));
}
}
}
$_SESSION['message'] = "Books Created Successfully";
}
?>
前-
如果用户在文本框中输入
bookno - 101 totalbook - 2
receiptno - 500 totalrec - 2
优惠券 - 700 总优惠券 - 2
然后输出如下 table。
在这个table中,优惠券号总是unique.and收据没有来两次,因为每张收据都有两张优惠券。
前-
书票券
101 500 - 700
101 500 - 701
101 - 501 - 702
101 - 501 - 703
102 - 502 - 704
102 - 502 - 705
102 - 503 - 706
102 - 503 - 707
现在第二次输入
书本代码 = 800 book_no2 = 802
收据代码 = 1600 Temp_receipt = 1602
优惠券代码 = 1800
Temp_coupon = 1802
然后生成如下输出
书票券 801 -1601 1801 801 -1601 -1802 801 -1602 -1803 801 -1602 -1804 802 -1603 -1805 802 -1603 -1806 802 -1604 -1807 802 -1604 -1808
我尝试了下面的代码但无法正常工作。
<?php
if(isset($_POST['save']))
{
$bookcode = $_POST['bookcode'];
$book_no2 = $_POST['book_no2'];
$receiptcode = $_POST['receiptcode'];
$receipt_no = $_POST['receipt_no'];
$couponcode = $_POST['couponcode'];
$coupon = $_POST['coupon'];
$Temp_receipt = $receiptcode + $receipt_no;
$Temp_coupon = $couponcode + $coupon;
for($row1=$bookcode+1;$row1<=$bookcode+$book_no2;$row1++)
{
for($row=$receiptcode+1;$row<=$Temp_receipt;$row++)
{
$query = $database->getRow("SELECT MAX(receipt_no) AS max1 FROM scheme_master;");
if($query['max1']=='')
{
$largestNumber = $receiptcode;
$top = $largestNumber + 1;
}
else
{
$largestNumber = $query['max1'];
$top = $largestNumber + 1;
}
$Pric = "";
$loopCount = 0;
for($row2=$couponcode+1;$row2<=$Temp_coupon;$row2++)
{
$query = $database->getRow("SELECT MAX(coupon) AS max2 FROM scheme_master;");
if($query['max2']=='')
{
$largestcoupon = $couponcode;
$coup = $largestcoupon + 1;
}
else
{
$largestcoupon = $query['max2'];
$coup = $largestcoupon + 1;
}
$value = $loopCount++ + 1;
$code = '- mths';
$Pric=$value.$code;
$insertrow = $database->insertRow("INSERT INTO scheme_master (book_no2,receipt_no,coupon)
VALUES (:book_no2,:receipt_no,:coupon)",
array(':receipt_no'=>$top,':book_no2'=>$row1,':coupon'=>$coup));
}
}
}
$_SESSION['message'] = "Books Created Successfully";
}
?>
我得到如下输出 table...
书票券
101 - 501 -701
101 - 502 -702
102 - 501 -701
102 - 502 -702
每次使用循环时,它都会重置回输入的值(例如,coupon_no)
您应该使用 bookno
、couponno
和 receiptno
的值作为起点,并使用 totalbook
、totalcoup
和 [=16] 的值=] 作为循环条件。边走边增加数字:
<?php
$bookno= $_POST['bookcode'];
$totalbook= $_POST['book_no2'];
$receiptno = $_POST['receiptcode'];
$totalrec= $_POST['receipt_no'];
$couponno= $_POST['couponcode'];
$totalcoup= $_POST['coupon'];
for ($book_counter=1; $book_counter<=$totalbook; $book_counter++)
{
for($rec_counter=1; $rec_counter<=$totalrec; $rec_counter++)
{
for($coup_counter=1; $coup_counter<=$totalcoup; $coup_counter++)
{
$insertrow = $database->insertRow(
"INSERT INTO scheme_master (book_no2,receipt_no,coupon) VALUES (:book_no2,:receipt_no,:coupon)",
array(':receipt_no'=>$receiptno,':book_no2'=>$bookno,':coupon'=>$couponno));
$couponno++;
}
$receiptno++;
}
$bookno++;
}
?>
注意这还没有经过测试,但它应该会让你走上正确的道路
编辑后的代码(带有关于未使用变量的注释)
<?php
if (isset($_POST['save']))
{
/*
* No checking for validity of values
*/
$bookcode = $_POST['bookcode'];
$book_no2 = $_POST['book_no2'];
$receiptcode = $_POST['receiptcode'];
$receipt_no = $_POST['receipt_no'];
$couponcode = $_POST['couponcode'];
$coupon = $_POST['coupon'];
$Temp_receipt = $receiptcode + $receipt_no;
$Temp_coupon = $couponcode + $coupon;
for ($row1 = $bookcode + 1; $row1 <= $bookcode + $book_no2; $row1++) // what happens if $bookcode or $book_no2 are negative? Why start at +1 instead of what user entered?
{
for ($row = $receiptcode + 1; $row <= $Temp_receipt; $row++) // what happens if $receiptcode or $receipt_no are negative? Why start at +1 instead of what user entered?
{
/*
* Each run of this query will slow down this whole code
*/
$query1 = $database -> getRow("SELECT if (receipt_no is null, 1,MAX(receipt_no)+1) AS max1 FROM scheme_master;");
$top = $query1['max1'];
/*
* Initialised but $Pric is never used apart from an assignment
*/
$Pric = "";
$loopCount = 0; // initialised and updated but not used anywhere in this code
for ($row2 = $couponcode + 1; $row2 <= $Temp_coupon; $row2++) // what happens if $couponcode or $coupon are negative? Why start at +1 instead of what user entered
{
/*
* Each run of this query will slow down this whole code
*/
$query2 = $database -> getRow("SELECT if (coupon is null, 1, MAX(coupon)+1) AS max2 FROM scheme_master;");
$coup = $query2['max2'];
/*
* $value is never used
*/
$value = $loopCount+=2; // the same as $value=$loopCount++ +1; No idea why it's here as it is not used at all in this code
/*
* These values are never used
*/
$code = '- mths'; //unused. As it never changes, should be initialised earlier
$Pric = $value.$code; //unused
$insertrow = $database -> insertRow("INSERT INTO scheme_master (book_no2,receipt_no,coupon) VALUES (:book_no2,:receipt_no,:coupon)", array(':receipt_no' => $top, ':book_no2' => $row1, ':coupon' => $coup));
}
}
}
$_SESSION['message'] = "Books Created Successfully";
}
?>