2013-07-02 83 views
0

我使用PHP連接並顯示來自ODBC MS Access數據庫的數據。我可以使用select語句顯示必要的數據,但我無法學習如何根據另一個數據集對這些數據進行分組。使用普通數據集對PHP ODBC結果進行分組?

這裏是我的代碼:

<? 
$handle = odbc_connect("Potter","",""); 
$results = odbc_exec($handle, "SELECT DISTINCT prodModelNo, prodSubType, prodCAD FROM product WHERE prodType NOT LIKE 'Plus' AND prodType = 'Fire' AND prodCAD LIKE '%CAD Drawing%' GROUP BY prodSubType"); 

     // Results List 
     odbc_fetch_row($results, 0); 
     while(odbc_fetch_row($results)){ 
      $cadString1  = odbc_result($results, "prodCAD"); 
      $cadString2  = str_replace('<img src=images/i_drawing.gif> <a href=fire/', '', $cadString1); 
      $cadURL   = str_replace(' target=_blank>CAD Drawing (zip format)</a>', '', $cadString2); 
      echo "<div id='file'><div id='filelink'><ul><li><a href='$cadURL'><img src='../images/download.png'/> Download</a> (zip format)</li></ul></div>"; 
      echo odbc_result($results, "prodModelNo"); 
      echo "<div class='clear'></div></div>"; 
     } 
?> 

該輸出準確的數據的列表。我想要做的是分組結果,這些結果共享一個共同的類別,並在每個組之間顯示該類別名稱。數據庫中類別名稱的列稱爲prodSubType。

我如何得到這個顯示正確?

回答

0

而不是GROUP BY prodSubType使用ORDER BY prodSubType - 所以你可以抓住所有物品。

然後,在你的代碼中,有一個BEFORE和AFTER變量來輸出標題差異(我不擅長解釋這個)。看例子(未經測試):

<?php 
$handle = odbc_connect("Potter","",""); 
$results = odbc_exec($handle, "SELECT DISTINCT prodModelNo, prodSubType, prodCAD FROM product WHERE prodType NOT LIKE 'Plus' AND prodType = 'Fire' AND prodCAD LIKE '%CAD Drawing%' ORDER BY prodSubType ASC"); 

// Results List 
odbc_fetch_row($results, 0); 
$oldHeader = ""; 
while(odbc_fetch_row($results)){ 
    $header = odbc_result($results, "prodSubType"); 
    if($header != $oldHeader) { 
    $oldHeader = $header; 
    echo "<h1>$header</h1>"; 
    } 
    $cadString1  = odbc_result($results, "prodCAD"); 
    $cadString2  = str_replace('<img src=images/i_drawing.gif> <a href=fire/', '', $cadString1); 
    $cadURL   = str_replace(' target=_blank>CAD Drawing (zip format)</a>', '', $cadString2); 
    echo "<div id='file'><div id='filelink'><ul><li><a href='$cadURL'><img src='../images/download.png'/> Download</a> (zip format)</li></ul></div>"; 
    echo odbc_result($results, "prodModelNo"); 
    echo "<div class='clear'></div></div>"; 
} 
?> 
+0

謝謝,這工作完美! – therealbiglou