2015-05-13 109 views
1

我正在嘗試在我的網站中創建一個屬性頁面,以顯示屬於交易者的產品,每件產品的銷售量以及已售出的某種產品的總價。創建統計頁面。 Php

我的代碼此刻創建了一個while循環,但是當它在數據庫中循環時,它會多次顯示這些項目(而不是每個產品只顯示一次),並且每個銷售的產品的總量未設置爲某個產品,它顯示了每個銷售產品的總金額。

這裏是我的select語句:

 $querytest = "SELECT * FROM PRODUCT 
        INNER JOIN TRADER 
        ON PRODUCT.Stall_ID=TRADER.Stall_ID 
        INNER JOIN ORDER_ITEMS 
        ON PRODUCT.Product_ID=ORDER_ITEMS.Product_ID 
        WHERE TRADER.Username='". $_SESSION['Username'] ."'"; 

     $testresult = oci_parse($connection, $querytest); 
     oci_execute($testresult) 

這是我while循環。我希望它只輸出數據庫中的產品一次。但它正在無數次地輸出它們。

while($row = oci_fetch_assoc($testresult)) { 
     $DName = $row ['NAME']; 
     $DProdesc = $row ['PRODUCT_DESC']; 
     $DIMAGEID = $row['IMAGEID']; 
     $DPrice = $row['PRICE']; 

     $price = $row['PRICE']; 

     $prodord = $row['PRODUCTQUANTITY']; 

     $totalprodord = $totalprodord + $prodord; 

     $totalprodprice = $totalprodord * $Dprice; 

     $quantity = $row['QUANTITY']; 

     $subtotal = ($price * $quantity); 

     $total = $total + $subtotal; 

     $stock = $stock + $quantity; 





      echo '<td> <img src="' . $DIMAGEID. '" height="50" width="50"/></td>'; 
      echo '<td>' .$DName. '</td>'; 
      echo '<td>' .$DProdesc. '</td>'; 
      echo '<td>' .$totalprodord. '</td>'; 
      echo '<td>&pound; ' .$DPrice. '</td>'; 
        echo '<td>&pound; ' . $totalprodprice . '</td>';'</tr>'; 
    } 


     ob_flush(); 
     ?> 

我的數據庫表:

CREATE TABLE TRADER(
    Trader_ID number (5) NOT NULL, 
    Trader_Name varchar2 (20), 
    FirstName varchar2 (10) NOT NULL, 
    LastName varchar2 (10) NOT NULL, 
    DOB date NOT NULL, 
    Username varchar2 (15) NOT NULL, 
    Password varchar2 (32) NOT NULL, 
    Phone varchar2 (10), 
    Email varchar2 (30) NOT NULL, 
    Stall_ID number (5) references STALL(Stall_ID), 
    Primary Key (Trader_ID)); 

CREATE TABLE PRODUCT(
    Product_ID number (5) NOT NULL, 
    Name varchar2 (30) NOT NULL, 
    Product_Desc varchar2 (50), 
    ImageID varchar2 (35), 
    Price number(5,2) NOT NULL, 
    Quantity number (10) NOT NULL, 
    Stall_ID number (5) references STALL(Stall_ID), 
    Product_Type_ID number (5) references PRODUCT_TYPE(Product_Type_ID), 
    Primary Key (Product_ID)); 

CREATE TABLE ORDER_ITEMS(
    Order_Items_ID number (5) NOT NULL, 
    TotalPrice number (5,2) NOT NULL, 
    ProductQuantity number (5) NOT NULL, 
    Day_ID number (5) references COLLECTION_DAY(Day_ID), 
    Time_ID number (5) references COLLECTION_TIME(Time_ID), 
    Customer_ID number (5) references CUSTOMER(Customer_ID), 
    Product_ID number (5) references PRODUCT(Product_ID), 
    Order_ID number (5) references CUSTOMER_ORDER(Order_ID), 
    Primary Key (Order_Items_ID)); 

任何人可以幫我得到正確顯示統計?

感謝

+0

HTML blob與您的問題無關。相反,解釋你的餐桌方案和內容。舉一個你想分組的例子。 – mario

+0

首先,我會說...分開你的HTML和PHP。其次,回聲與你的問題無關。第三:請提供更多信息,如數據庫方案 – Robin

回答

1

您需要在您的查詢總結你的號碼,或者你有總結數字和代碼跳過相同的產品。你既沒有做。

嘗試用這樣的替換查詢:

SELECT prod.product_id, prod.name, 
prod.quality-sum(orders.quality) AS Stock_Left, 
AVG(orders.price) AS Sold_price, 
SUM(orders.price * orders.quality) AS Sold_Amount, 
Sum(orders.quality) AS Total_Sold 
FROM product prod INNER JOIN Order_Items orders 
ON prod.prod_id = orders.prod_id 
INNER JOIN trader 
ON prod.stall_id = trader.stall_id 
WHERE trader.Username='". $_SESSION['Username'] ."'" 

你可以擺脫在環路大部分的計算。