Для Ex-
Если пользователь вводит текстовое поле, например
bookno – 101 totalbook – 2
получениеno – 500 totalrec – 2
купон – 700 totalcoup – 2
то вывод поступает, как показано ниже.
В этой таблице купон не всегда уникален. И квитанция не приходит дважды bcoz, каждая квитанция имеет два купона.
Для Ex-
купон на получение книги
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
Каждый раз, когда вы используете цикл, он возвращается к введенному значению (например, 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 Это не было протестировано, но оно должно установить вас на правильном пути
Отредактированный код (с комментариями относительно неиспользуемых переменных)
<?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"; } ?>
не<?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"; } ?>
не<?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"; } ?>