0

我正在使用 PHP 连接并显示来自 ODBC MS Access 数据库的数据。我可以使用 select 语句显示必要的数据,但我无法学习如何根据另一个数据集对这些数据进行分组。

这是我的代码:

<?
$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。

我怎样才能让它正确显示?

4

1 回答 1

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>";
}
?>
于 2013-07-02T20:37:57.583 回答