2012-03-17 77 views
0

我的應用程序需要創建一個庫存管理表,其中有很多項目分爲子組。對於和想法,庫存管理表如下所示:如何擺脫幾個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查詢這確實對我來說,這是可怕的。在一個要點,我在做什麼是以下幾點:

  1. 選擇第一類,並開始while循環 //回聲爲主要類別
  2. 選擇子類對應於主類別和開始時再次循環。 // echo作爲子類別
  3. 選擇子類別中的所有項目並啓動一個while循環。
  4. 選擇第一個地點。
  5. 從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>"; 
      } 
     } 
    } 
+1

它可能會做,與一個單一的查詢,和一個單一的循環。請發佈你的數據庫結構。 – bfavaretto 2012-03-17 19:01:42

+0

當你說select時,你的意思是執行查詢嗎?我以爲你已經擁有了內存中的所有數據? – mindandmedia 2012-03-17 19:04:07

+0

@mindandmedia對不起,這意味着執行查詢 – Namit 2012-03-17 19:04:51

回答

1

如何這樣的事情 -

<?php 

$locations = array('Location 1', 'Location 2', 'Location 3'); 
$locationCols = array(); 
$locationList = array(); 

foreach ($locations as $location) { 
    $locationColName = preg_replace('[^a-z0-9]', '_', strtolower($location)); 
    $location = mysql_real_escape_string($location); 
    $locationCols[] = "SUM(IF(Office.OfficeTitle = '$location', Stock_Management.Quantity, 0)) AS `$locationColName`"; 
    $locationList[] = "'$location'"; 
} 
$locationCols = implode(', ', $locationCols); 
$locationList = implode(',', $locationList); 

$sql = "SELECT Board.Title AS BoardTitle, Sub_Category.Title AS SubCatTitle, Book.title AS BookTitle, $locationCols, SUM(Stock_Management.Quantity) AS Total 
     FROM Board 
     INNER JOIN Sub_Category 
      ON Board.Board_ID = Sub_Category.Category_ID 
     INNER JOIN Book 
      ON Sub_Category.Sub_Category_ID = Book.sub_category_id 
     INNER JOIN Office 
     LEFT JOIN Stock_Management 
      ON Book.Book_ID = Stock_Management.Book_ID 
      AND Office.OfficeID = Stock_Management.Location_ID 
     WHERE Board.Company_ID = 2 
     AND Office.OfficeTitle IN ($locationList) 
     GROUP BY Board.Title, Sub_Category.Title, Book.title"; 

$result = mysql_query($sql); 

$prevBoard = ''; 
$prevSubCat = ''; 

echo '<table>'; 

while ($row = mysql_fetch_object($result)) { 

    // if new board print board 
    if ($prevBoard != $row->BoardTitle) { 
     echo '<tr class="sub-heading" style="background: rgba(76, 178, 255, 0.1)"><td colspan="">' . $row->BoardTitle . '</td></tr>'; 
    } 
    $prevBoard = $row->BoardTitle; 

    if ($prevSubCat != $row->SubCatTitle) { 
     echo '<tr style="background: rgba(149, 255, 145, 0.10)"><td colspan=""><b>' . $row->SubCatTitle . '</b></td></tr>'; 
    } 
    $prevSubCat = $row->SubCatTitle; 

    // print product row 
    echo '<tr>'; 
    echo "<td>{$row->BookTitle}</td>"; 

    foreach ($locations as $location) { 
     $locationColName = preg_replace('[^a-z0-9]', '_', strtolower($location)); 
     echo "<td>{$row->$locationColName}</td>"; 
    } 

    echo "<td>{$row->Total}</td>"; 
    echo '</tr>'; 
} 

echo '<table>'; 
+0

我不能使用它,因爲用戶可以選擇他想要查看數據的位置。所以我不能使用''位置1'等。感謝您的答案,雖然 – Namit 2012-03-17 19:45:00

+0

這就是爲什麼我說你可以建立位置字段動態基於位置選擇。用戶提交的地點如何?它是位置ID的數組嗎? – nnichols 2012-03-17 20:18:33

+0

它們使用ajax,'echo $ locations'發送的結果在'Location1,Location2,Location3'等。 – Namit 2012-03-17 23:04:13