0

I have a table in which data is entered by the user. The rows are created dynamically. The value given by the user is sent to the DB(Oracle 11g) and based on that other values are fetched and inserted into the table. The code is like this:

<!DOCTYPE HTML>
<html>
    <body>
        <?php
            $code = $_POST['code'];
            $qty = $_POST['qty'];
            foreach ($_POST['code'] as $code => $c) {
                //print $c . "&nbsp;&nbsp;&nbsp;" . $qty[$code] . "<br>"; 
            }
            $link = oci_connect('hd','hd', 'localhost/mydb');
            if(!$link) {
                $e = oci_error();
                exit('Connection error  ' . $e['message']);
            }
            foreach ($_POST['code'] as $code => $c)
            {
                $q1 = "select PROD_NAME, PROD_COST from PRODUCT where PROD_ALIAS = :bv_code";
                $q1parse = oci_parse($link, $q1);
                oci_bind_by_name($q1parse, ':bv_code', $c);
                oci_execute($q1parse);
                while($row = oci_fetch_array($q1parse))
                    PRINT $row['PROD_NAME'] . "&nbsp;&nbsp;&nbsp;&nbsp;" . $row['PROD_COST'] . 
                    "&nbsp;&nbsp;&nbsp;&nbsp;" . $qty[$code] . "&nbsp;&nbsp;&nbsp;&nbsp;" . 
                    ($row['PROD_COST']*$qty[$code]) . "<br>";
            }
        ?>
        <script type = "text/javascript" >
            function addRow()
            {
                var table = document.getElementById('order');
                var row = table.insertRow(-1);
                var cell1 = row.insertCell(0);
                var cell2 = row.insertCell(1);
                var cell3 = row.insertCell(2);
                var cell4 = row.insertCell(3);
                var cell5 = row.insertCell(4);
                cell1.innerHTML = "<input type = 'text' name = 'code[]' /> "; 
                cell2.innerHTML = "";
                cell3.innerHTML = "";
                cell4.innerHTML = "<input type = 'text' name = 'qty[]' />";
                cell5.innerHTML = "";

            }

            function delRow(r) 
            {
                document.getElementById('order').deleteRow(-1);
            }   

        </script>
        <p><strong> Order Details </strong></p>
        <form action = 'm3.php' method = 'POST' >
            <table id = "order" border = 1 border_collapse = "collapse" >
                <tr>
                    <th> Item Code </th>
                    <th> Name </th>
                    <th> Price </th>
                    <th> Quantity </th>
                    <th> Total </th>
                </tr>
            </table>
            <input type = 'submit' name = 'submit' value = 'Submit' />
            <input colspan = '2' type = "button" value = "Add Row" onclick = "addRow('order')" />
            <input colspan = '2' type = "button" value = "Delete Row" onclick = "delRow('order')" />
        </form>
    </body>
</html>

The user provides the value of "CODE" and "Quantity". The "CODE" is sent to the DB and other values of the product are fetched. The problem is I want to display the fetched values in the same table. Right now I am displaying it outside the table. Also, I want a column in the end displaying the total bill. As in, the total of (price*qty). How can I do this?

Karan Gupta
  • 529
  • 2
  • 7
  • 21

1 Answers1

2

Instead of placing the entire foreach loop up in the page, you should place it inside the <table> ... </table> like this:

// your code
<table id = "order" border = 1 border_collapse = "collapse" >
    <tr>
        <th> Item Code </th>
        <th> Name </th>
        <th> Price </th>
        <th> Quantity </th>
        <th> Total </th>
    </tr>
    <?php
        $sumTotal = 0;
        foreach ($_POST['code'] as $code => $c){
            $q1 = "select PROD_NAME, PROD_COST from PRODUCT where PROD_ALIAS = :bv_code";
            $q1parse = oci_parse($link, $q1);
            oci_bind_by_name($q1parse, ':bv_code', $c);
            oci_execute($q1parse);
            while($row = oci_fetch_array($q1parse)){
                ?>
                <tr>
                    <td><?php echo $c; ?></td>
                    <td><?php echo $row['PROD_NAME']; ?></td>
                    <td><?php echo $row['PROD_COST']; ?></td>
                    <td><?php echo $qty[$code]; ?></td>
                    <td><?php echo $row['PROD_COST']*$qty[$code]; ?></td>
                </tr>
                <?php
                $sumTotal += $row['PROD_COST']*$qty[$code];
            }
        }
    ?>
    <tr>
        <td>Sum Total</td>
        <td colspan="4"><?php echo $sumTotal; ?></td>
    </tr>
</table>
// your code
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • oh that's great. It works but how to do the total? As in the total amount? – Karan Gupta Jul 02 '17 at 11:10
  • @KaranGupta I've updated my answer to address your comment i.e. to accommodate the total amount calculation. Hopefully this will resolve your issue. – Rajdeep Paul Jul 02 '17 at 11:19
  • Its not giving the desired result. I get an error "Undefined variable sumTotal in line no. 70" i.e. the line where we declare 'sumTotal'. But it displays the result too, though out of the table – Karan Gupta Jul 02 '17 at 11:35
  • ! ) Notice: Undefined variable: sumTotal in E:\xampp\htdocs\myfiles\m3.php on line 70 Call Stack # Time Memory Function Location 1 0.0005 140960 {main}( ) ...\m3.php:0 – Karan Gupta Jul 02 '17 at 11:35
  • I fixed the error , you did not put ";" while declaring sumTotal. But still it gives the above error. – Karan Gupta Jul 02 '17 at 11:37
  • @KaranGupta Ah, you're right, I forgot to put semicolon there, fixed it. Please *accept* the answer if it resolved your issue. [How to accept answer on Stack Overflow?](https://meta.stackexchange.com/a/5235) – Rajdeep Paul Jul 02 '17 at 11:55
  • i did accept it but its not giving the desired result. It gives the above error. If i Initialize it to 0 then it does not do the total. – Karan Gupta Jul 02 '17 at 11:57
  • @KaranGupta Are you still getting *Undefined variable: sumTotal* error? If so, paste your code on [pastebin.com](https://pastebin.com/) and give me it's link here. – Rajdeep Paul Jul 02 '17 at 11:59
  • yup still getting the error. i pasted the code in the above link. – Karan Gupta Jul 02 '17 at 12:11
  • @KaranGupta You didn't went through my code correctly. You didn't initialize `$total` to `0` in your code, do `$total = 0;` before the `foreach` loop. Here's the updated code, [https://pastebin.com/T6qQ4G8w](https://pastebin.com/T6qQ4G8w) – Rajdeep Paul Jul 02 '17 at 12:41
  • I did initialize it but removed it as it was not doing the total. But I initialized it at the wrong place. Thanks, it works like a charm. – Karan Gupta Jul 02 '17 at 12:45
  • Any idea how to print it to a thermal printer? Or any place where I could find it? – Karan Gupta Jul 02 '17 at 12:50
  • @KaranGupta Just do ctrl+p and it'll do the job. Otherwise convert your HTML document to PDF first and then get it printed, here's a good read: [https://stackoverflow.com/q/733219/5517143](https://stackoverflow.com/q/733219/5517143) – Rajdeep Paul Jul 02 '17 at 12:57
  • Ok il do that. I was hoping to make a button and clicking on it will show the invoice and then print to a thermal printer. Il read it. Thanks :D – Karan Gupta Jul 02 '17 at 12:59