Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用自定义代码生成多行的php代码_Php_Mysql_Pdo - Fatal编程技术网

使用自定义代码生成多行的php代码

使用自定义代码生成多行的php代码,php,mysql,pdo,Php,Mysql,Pdo,对于Ex- 如果用户在文本框中输入,如 第101册总计第2册 接收编号-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 现在是第二次输入时 图书编码=800

对于Ex-

如果用户在文本框中输入,如

第101册总计第2册

接收编号-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

现在是第二次输入时

图书编码=800 图书编号2=802

接收代码=1600 临时收据=1602

耦合码=1800
临时优惠券=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

每次使用循环时,它都会重置回输入的值(例如,优惠券编号)

您应该使用
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这尚未经过测试,但它应该将您设置在正确的路径上

编辑的代码(带有关于未使用变量的注释)


您得到了什么输出?如果不知道“工作不正常”是什么意思,帮助你有点困难。知道输出应该是什么也很有用。我在上面更新了我的输出。plz帮助。这是一个与最初发布的问题不同的问题。关闭这个问题,开始一个新的问题,提供所有你能提供的信息,在你的代码中添加日志来显示各种变量的值(在你的问题中也给出这个值)我直接创建这个输出不是从代码…但是我需要从上面输入的代码中得到这个输出。。如果你这样做,我会很感激你。当你运行代码时,你得到了什么?循环每次迭代的相关变量值是多少?添加调试代码以查找错误。说“它进入一个无限循环”而不提供任何实际发生的背景是没有任何帮助的没有先生,现在这段代码工作正常,没有进入任何无限循环,但我的意思是上面的代码没有得到输出,因为我发送给你上面的输入…做一些调试日志,并张贴在这里!我不知道你在期待什么,你输入了什么值,你在使用什么代码或者其他什么。它从一个无限循环一直到工作,你没有告诉我你做了什么或你在使用什么先生,我现在发布我的完整代码…这正是我想要的…但问题是我在收据和优惠券代码循环中使用if-else ok…第一次当数据库为空时,它会生成正确的输出…正如我想要的…ok
<?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";
}
?>