1

From this table

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

How can i SELECT each class distinctly and under of it the title and content in this 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

Please use this to relate a hint about the modification of the answer. https://stackoverflow.com/a/43507106/7490349

Community
  • 1
  • 1
Calibur Victorious
  • 638
  • 1
  • 6
  • 20
  • output them in class order from the DB. Create a "Class: 1" div for the first class. Keep a record of the current class ID. Then loop through each row and output the title/contents divs. At each loop iteration, check whether the class ID has changed from the previous row. If it has, create a new "Class : x" div. That's really the simplest way. – ADyson Apr 25 '17 at 12:46
  • @ADyson an example would be very useful at this point. – Calibur Victorious Apr 25 '17 at 12:48

1 Answers1

1

According to your reference link and your output hope this help:

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>";
B. Desai
  • 16,414
  • 5
  • 26
  • 47