-2

For Ex-

If user input in textbox like

bookno - 101 totalbook - 2

receiptno - 500 totalrec - 2

coupon - 700 totalcoup - 2

then output comes like below table.

In this table, coupon no are always unique.and receipt no comes twice bcoz each receipt has two coupon alloted.

For Ex-

book receipt coupon

101 500 - 700

101 500 - 701

101 - 501 - 702

101 - 501 - 703

102 - 502 - 704

102 - 502 - 705

102 - 503 - 706

102 - 503 - 707

Now second time when i input

bookcode = 800 book_no2 = 802

receiptcode = 1600 Temp_receipt = 1602

couponcode = 1800
Temp_coupon = 1802

then generate output like below

book receipt coupon 801 -1601 1801 801 -1601 -1802 801 -1602 -1803 801 -1602 -1804 802 -1603 -1805 802 -1603 -1806 802 -1604 -1807 802 -1604 -1808

i try below code but not working properly.

    <?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";                        
    }       

?>

I am getting output like below table...

book receipt coupon

101 - 501 -701

101 - 502 -702

102 - 501 -701

102 - 502 -702

3
  • 2
    What output do you get? Without knowing what "not working properly" means, it's a bit difficult to help you. It's also useful to know what should be the output Commented Feb 17, 2015 at 11:56
  • i updated my output above...plz help .. Commented Feb 17, 2015 at 12:05
  • This is a different question to originally posted. Close this one off, start a new question, supply all the information you can, add logging to your code to show values of various variables (and give that in your question too) Commented Feb 17, 2015 at 14:58

2 Answers 2

0

Each time you use a loop it resets back to the entered value (e.g., coupon_no)

You should use the values for bookno, couponno and receiptno as starting points and the values for totalbook, totalcoup and totalrec as the loop criteria. Increment the numbers as you go:

<?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 This has not been tested but it should set you on the right path

Sign up to request clarification or add additional context in comments.

10 Comments

i create this output directly not from code ...but i need this output from code of above input.. i will be grateful to you if you do this..
What do you get when you run the code? What are the values for the relevant variables on each iteration of the loop? Add debug code to find errors. Saying "It goes into an infinite loop" without giving any background as to what is actually happening is not helpful in the slightest
no sir now this code working properly, not going in any infinite loop but my mean is above code not getting output as i send you for above input...
DO SOME DEBUG LOGGING AND POST IT HERE! I have no idea what you're expecting, what values you have entered, what code you are using or anything. It's gone from an infinite loop through to working without you telling me what you've done or what you're using
sir i post my full code now...this is working exactly as i want........but the prob is i use if else inside receipt and coupon code loop ok....first time when database was empty it generates proper output.....as i want..ok
|
0

Edited code (with comments regarding unused variables)

<?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";
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.