在格式化的html表

时间:2016-12-26 20:01:30

标签: php html mysql mysqli html-table

我有一个mysqli查询返回以下结果:

+--------+------+-----------+---------+-------+----------+
| Agent  | Date | operation | article |  num  | quantity |
+--------+------+-----------+---------+-------+----------+
| Albert | D1   | SL1       | A       |  0001 |      300 |
| Albert | D1   | SL1       | A       |  0002 |      400 |
| Albert | D3   | SL2       | B       | 00046 |      100 |
| Robert | D3   | SL4       | A       |  1231 |      400 |
| Robert | D3   | SL5       | C       |   888 |       20 |
| Robert | D2   | SL3       | B       |  9999 |       90 |
+--------+------+-----------+---------+-------+----------+

查询与AGENT ASC, DATE ASC, Operation ASC, ARTCLE ASC

一起订购

我试图以下列方式在格式化的HTML表格中打印结果

Agent: ALBERT
     DATE : D1
       Operation: SL1
          ARTICLE : A
             num: 0001 | Quantity: 300
             num: 0002 | Quantity: 400
     DATE : D3
       Operation: SL2
          ARTICLE : B
             num: 00046 | Quantity:100
Agent: Robert
     DATE: D3
       Operation: SL4
          ARTICLE : A
             num: 1231  | Quantity: 400
       Operation: SL5
          ARTICLE : C
             num: 888  | Quantity: 20
     DATE: DX
       Operation: SL3
          ARTICLE : B
             num: 9999  | Quantity: 90

我尝试过这样做,但没有奏效:

$prevAgent="";
$prevDate="";
$prevOperation="";
$prevArticle="";
$prevNum="";
while ($row = mysqli_fetch_array($result)) {
     if ($row['Agent']!= $prevAgent) {
                   echo "<tr><td>".$row['Agent']."</td><td></td><td></td><td></td><td></td><td></td></tr>";
                                }
     if ($row['date']!= $prevDate) {
                   echo "<tr><td></td><td>".$row['date']."</td><td></td><td></td><td></td><td></td></tr>";
                                }
     if ($row['Operation']!= $prevOperation) {
                   echo "<tr><td></td><td></td><td>".$row['Operation']."</td><td></td><td></td><td></td></tr>";
                                }
     if ($row['Article']!= $prevArticle) {
                   echo "<tr><td></td><td></td><td></td><td>".$row['Article']."</td><td></td><td></td></tr>";
                                }
       if ($row['Num']!= $prevNum) {
                  echo "<tr><td></td><td></td><td></td><td></td><td>".$row['numLot']."</td><td>".$row['quantity']."</td></tr>";
                                }

  $prevAgent = $row['Agent'];
  $prevDate      = $row['date'];
  $prevOperation    = $row['operation'];
  $prevArticle      = $row['article'];
  $prevNum      = $row['num'];
      }
    endif; ?>

我一直在讨价还价。如何以先前的格式打印结果?

3 个答案:

答案 0 :(得分:1)

您需要执行两个分层移动:

$obj = [[
    "Agent" => "Albert",
    "Date" => "D1",
    "operation" => "SL1",
    "article" => "A",
    "num" => "0001",
    "quantity" => "300"
], [
    "Agent" => "Albert",
    "Date" => "D1",
    "operation" => "SL1",
    "article" => "A",
    "num" => "0002",
    "quantity" => "400"
], [
    "Agent" => "Albert",
    "Date" => "D3",
    "operation" => "SL2",
    "article" => "B",
    "num" => "00046",
    "quantity" => "100"
], [
    "Agent" => "Robert",
    "Date" => "D3",
    "operation" => "SL4",
    "article" => "A",
    "num" => "1231",
    "quantity" => "400"
], [
    "Agent" => "Robert",
    "Date" => "D3",
    "operation" => "SL5",
    "article" => "C",
    "num" => "888",
    "quantity" => "20"
], [
    "Agent" => "Robert",
    "Date" => "D2",
    "operation" => "SL3",
    "article" => "B",
    "num" => "9999",
    "quantity" => "90"
]];
$agent = [];
foreach ($obj as $val) {
    $agent[$val["Agent"]][] = $val;
}
$agentDate = [];
foreach ($agent as $agent => $val) {
    foreach ($val as $agDate => $value) {
        $agentDate[$agent][$value["Date"]] = $value;
    }
}

完成此操作后,您将以以下形式获取它:

Array
(
  [Albert] => Array
    (
      [D1] => Array
        (
          [Agent] => Albert
          [Date] => D1
          [operation] => SL1
          [article] => A
          [num] => 0002
          [quantity] => 400
        )

      [D3] => Array
        (
          [Agent] => Albert
          [Date] => D3
          [operation] => SL2
          [article] => B
          [num] => 00046
          [quantity] => 100
        )

    )

  [Robert] => Array
    (
      [D3] => Array
        (
          [Agent] => Robert
          [Date] => D3
          [operation] => SL5
          [article] => C
          [num] => 888
          [quantity] => 20
        )

      [D2] => Array
        (
          [Agent] => Robert
          [Date] => D2
          [operation] => SL3
          [article] => B
          [num] => 9999
          [quantity] => 90
        )

    )

)

最后,如果你这样做:

foreach ($agentDate as $agent => $details) {
    echo "Agent: $agent\n";
    foreach ($details as $date => $value) {
        echo "\tDate: $date\n";
        echo "\t\tOperation: {$value["operation"]}\n";
        echo "\t\t\tARTICLE : {$value["article"]}\n";
        echo "\t\t\t\tnum : {$value["num"]}\n";
    }
}

您将获得输出:

Agent: ALBERT
     DATE : D1
       Operation: SL1
          ARTICLE : A
             num: 0001 | Quantity: 300
             num: 0002 | Quantity: 400
     DATE : D3
       Operation: SL2
          ARTICLE : B
             num: 00046 | Quantity:100
Agent: Robert
     DATE: D3
       Operation: SL4
          ARTICLE : A
             num: 1231  | Quantity: 400
       Operation: SL5
          ARTICLE : C
             num: 888  | Quantity: 20
     DATE: DX
       Operation: SL3
          ARTICLE : B
             num: 9999  | Quantity: 90

答案 1 :(得分:1)

您尝试的方式很好,只需要正确引用字段名称:它们区分大小写。

$row['Date']$row['date']是两回事,$row['Num']$row['numLot'] ......等等。

确保if条件,echo语句和$prevXXXX分配中的密钥相同。一旦您使用了查询生成的确切密钥,它就会起作用。

答案 2 :(得分:-2)

$data = array();
$data [] = array(
    'Agent' => 'Albert',
    'Date' => 'D1',
    'operation' => 'SL1',
    'article' => 'A',
    'num' => '0001',
    'quantity' => '300',
);
$data [] = array(
    'Agent' => 'Albert',
    'Date' => 'D1',
    'operation' => 'SL1',
    'article' => 'A',
    'num' => '0002',
    'quantity' => '400',
);
$data [] = array(
    'Agent' => 'Albert',
    'Date' => 'D3',
    'operation' => 'SL2',
    'article' => 'B',
    'num' => '00046',
    'quantity' => '100',
);
$data [] = array(
    'Agent' => 'Robert',
    'Date' => 'D3',
    'operation' => 'SL4',
    'article' => 'A',
    'num' => '1231',
    'quantity' => '400',
);
$data [] = array(
    'Agent' => 'Robert',
    'Date' => 'D3',
    'operation' => 'SL5',
    'article' => 'C',
    'num' => '888',
    'quantity' => '20',
);
$data [] = array(
    'Agent' => 'Robert',
    'Date' => 'D2',
    'operation' => 'SL3',
    'article' => 'B',
    'num' => '9999',
    'quantity' => '90',
);

//Agent
$filteredAgent = array();
foreach ($data as $value) {
    $filteredAgent[$value['Agent']][$value['Date']][$value['operation']][$value['article']][] = $value;
}
echo "<pre>";
print_r($filteredAgent);

foreach ($filteredAgent as $agent => $agentData) {
    echo "Agent : $agent";
    foreach ($agentData as $date => $dateData) {
        echo "<br>";
        echo "\tDate : $date";
        foreach ($dateData as $operation => $operationData) {
            echo "<br>";
            echo "\t\tOperation : $operation";
            foreach ($operationData as $article => $articleData) {
                echo "<br>";
                echo "\t\t\tarticle : $article";
                foreach ($articleData as $finalData) {
                    echo "<br>";
                    echo "\t\t\t\tnum : " . $finalData['num'] . " | Quantity :" . $finalData['quantity'];
                }
            }
        }
    }
    echo "<br>";
}