2017-07-02 83 views
0

我有一個表格,用戶輸入數據。行是動態創建的。用戶給出的值被髮送到數據庫(Oracle 11g),並根據其他值被提取並插入到表中。代碼如下:從數據庫中獲取值並在html表格中顯示,包括PHP

<!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> 

用戶提供「CODE」和「Quantity」的值。 「代碼」被髮送到數據庫並獲取產品的其他值。問題是我想在同一個表中顯示提取的值。現在我在桌子外面顯示它。另外,我想在最後一欄顯示總賬單。如在,總價(價格*數量)。我怎樣才能做到這一點?

回答

2

不是將在整個頁面的內容foreach圈起來的,你應該把它放在<table> ... </table>像這裏面:

// 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 
+0

哦這是偉大的。它的工作原理,但如何做到這一點?如在總金額? –

+0

@KaranGupta我已經更新了我的回答,以解決您的意見,即容納總金額計算。希望這會解決你的問題。 –

+0

它沒有給出想要的結果。我得到一個錯誤「第70行的未定義變量sumTotal」,即我們聲明'sumTotal'的那一行。但是它也顯示了結果,雖然在表 –

相關問題