对于前 -
如果用户在文本框中输入
bookno - 101 totalbook - 2
receiptno - 500 totalrec - 2
优惠券 - 700 totalcoup - 2
然后输出如下表所示。
在此表格中,优惠券号码始终是唯一的。收据没有两次bcoz每张收据都有两张优惠券。
对于前 -
预订收据优惠券
101 500 - 700
101 500 - 701
101 - 501 - 702
101 - 501 - 703
102 - 502 - 704
102 - 502 - 705
102 - 503 - 706
102 - 503 - 707
现在我第二次输入
bookcode = 800 book_no2 = 802
receiptcode = 1600 Temp_receipt = 1602
couponcode = 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";
}
?>
我的输出如下表所示......
预订收据优惠券
101 - 501 -701
101 - 502 -702
102 - 501 -701
102 - 502 -702
答案 0 :(得分:0)
每次使用循环时,它都会重置为输入的值(例如,coupon_no)
您应该使用bookno
,couponno
和receiptno
的值作为起点以及totalbook
,totalcoup
和totalrec
的值作为循环标准。随时增加数字:
<?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++;
}
?>
NB 这尚未经过测试,但应该让您走上正确的道路
答案 1 :(得分:0)
已编辑的代码(包含有关未使用变量的注释)
<?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";
}
?>