根据另一行值对行值进行排序,而不重复

时间:2017-04-25 12:43:36

标签: php

从此表

[   id    -   title   -   content  -   class]
[    1    -     t1    -     p1     -     1  ]
[    2    -     t2    -     p6     -     1  ]
[    3    -     t3    -     p5     -     2  ]
[    4    -     t4    -     p8     -     3  ]

我如何明确地选择每个类,并在其下面标记此HTML中的标题和内容

<div>
  <div>Class: 1</div>
     <div>
        <div>t1, p1</div>
        <div>t2, p6</div>
     </div>
  <div>Class: 2</div>
     <div>
        <div>t3, p5</div>
     </div>
  <div>
  <div>Class: 3</div>
     <div>
        <div>t4, p8</div>
     </div>
  <div>
</div

请使用此链接来提示有关修改答案的提示。 https://stackoverflow.com/a/43507106/7490349

1 个答案:

答案 0 :(得分:1)

根据你的参考链接和你的输出希望这个帮助:

SELECT title, content, class FROM your_table ORDER BY class, title



$previous_class = null;  // $previous_class will store the class of the previous row. It's
                                     // initialized to null because there's no previous row at first.

    echo "<div>";
    $end_div = "";
    while ($row = some_fetch_function()) {

        // compare the current row's class to the previous row's class, and
        // output the class label if it has changed.

        if ($row['class'] !== $previous_class) {
            echo $end_div;
            echo "<div>Class: $row[class]</div>";
            echo "<div>";

        }
        $end_div = "</div>";
        $previous_class = $row['class'];
        echo "<div>$row[title], $row[content]</div>";

    }
    echo $end_div;
    echo "</div>";