我的應用程序需要創建一個庫存管理表,其中有很多項目分爲子組。對於和想法,庫存管理表如下所示:如何擺脫幾個while循環和更好的顯示結果的想法
Location1 Location2 Total
Desktops // Main Category
Microsoft //Sub-Category
Windows 7 23000 150000 173000
Office 2011 203300 3002 206302
....
Apple //Sub-Category
OS Snow Leopard 4000 3000 7003
OS Lion 39494 40034 79528
...
Tablets //Main Category
Lenovo //Sub-Cateogry
LX-243 3434 4399 7833
...
這是表應該是什麼樣子可視化,現在我有一個巨大的MySQL查詢這確實對我來說,這是可怕的。在一個要點,我在做什麼是以下幾點:
- 選擇第一類,並開始while循環 //回聲爲主要類別
- 選擇子類對應於主類別和開始時再次循環。 // echo作爲子類別
- 選擇子類別中的所有項目並啓動一個while循環。
- 選擇第一個地點。
- 從3中選擇當前項目,從4中選擇當前位置並回顯項目。 //回顯項目名稱和位置中的數量。
現在我的問題是有更好的方式來顯示這比使用幾個while循環,我嘗試使用函數,但他們也變得一團糟。此外,我不知道我應該在哪裏執行計算,以獲得最後一列的總數。
數據庫結構:
Category: CategoryID, Description
Sub-Cateogry: Sub-Category_ID, Category_ID, Description
Item: Item_ID, Sub-category_id, Description
Location: Location_ID, Description
Stock_Management: Item_ID, Location_ID, Quantity
代碼的要求:
$sql = mysql_query("select Board_ID, Title from Board where Company_ID = '$company_id' order by Title");
while($row = mysql_fetch_array($sql)) {
$curr_ID = $row[0];
$curr_category = $row[1];
echo "<tr class='sub-heading' style='background: rgba(76, 178, 255, 0.1)'><td colspan='".$count."'>".$curr_category."</td></tr>";
$sql1 = mysql_query("select Sub_Category_ID, Title from Sub_Category where Category_ID = '$curr_ID' order by Title");
while($row1 = mysql_fetch_array($sql1)) {
$curr_sub_cat_id = $row1[0];
$curr_sub_cate = $row1[1];
echo "<tr style='background: rgba(149, 255, 145, 0.10'><td colspan='".$count."'><b>".$curr_sub_cate."</b></td></tr>";
$sql2 = mysql_query("select Book_ID, title from Book where sub_category_id = '$curr_sub_cat_id' Order by title");
while($row2 = mysql_fetch_array($sql2)) {
$curr_book = $row2[0];
echo "<tr><td>".$row2[1]."</td>";
$sql4 = mysql_query("select OfficeID, OfficeTitle from Office where OfficeTitle IN ('$locations')");
while($row3 = mysql_fetch_array($sql4)) {
$curr_location = $row3[0];
$sql3 = mysql_query("select Quantity from Stock_Management where Book_ID = '$curr_book' and Location_ID = '$curr_location'");
while($row3 = mysql_fetch_array($sql3)) {
echo "<td>".$row3[0]."</td>";
}
}
echo "</tr>";
}
}
}
它可能會做,與一個單一的查詢,和一個單一的循環。請發佈你的數據庫結構。 – bfavaretto 2012-03-17 19:01:42
當你說select時,你的意思是執行查詢嗎?我以爲你已經擁有了內存中的所有數據? – mindandmedia 2012-03-17 19:04:07
@mindandmedia對不起,這意味着執行查詢 – Namit 2012-03-17 19:04:51