Removing duplicate list items retrieved from the database

I have some problems with the list that should be displayed on the page.

This is what is currently displayed on the page.

Students

  • Student 3
    01-02-2016 from 08:30 a.m. to 2:30 p.m.
  • Student
    01-06-2016 from 10:30 a.m. to 3:30 p.m.
  • Student 1
    01-08-2016 from 05:30 to 16:30.
  • Student 3
    03-05-2016 from 08:30 a.m. to 10:30 a.m.
  • Student 2
    01-1-2016 from 13:30 to 18:30.
  • Student 1
    01-02-2016 from 16:30 to 20:30.

It should look like this:
Students

  • Student 1
    01-02-2016 from 16:30 to 20:30
    01-06-2016 from 10:30 to 15:30
    01-08-2016 from 05:30 to 16:30

  • 2
    01-1-2016 13:30 18:30.

  • 3
    01-02-2016 08:30 14:30
    03-05-2016 08:30 10:30

:

<div id="preferences">
    <h2>Leerlingen</h2>
    <?php
    $sql2 = "SELECT leerlingen.firstname, leerlingen.lastname, 
             voorkeuren.start, voorkeuren.end, voorkeuren.title FROM 
             leerlingen JOIN voorkeuren ON leerlingen.uid=voorkeuren.title";

    $result2 = $db->query($sql2);

    while ($row = $result2->fetch_assoc()) {

      ?>
        <ul>
            <li><h4><?= $row['firstname'] . " " . $row['lastname'];?></h4></li>
            <?= "Van " .  date('j-m-Y H:i', strtotime($row['start'])) . " tot " . date('H:i', strtotime($row['end'])) ?>
        </ul>

      <?php
    }
    ?>
</div>

, , , .

+4
2

, html

$sql2 = "SELECT leerlingen.firstname, leerlingen.lastname, voorkeuren.start, voorkeuren.end, voorkeuren.title
FROM leerlingen
JOIN voorkeuren
ON leerlingen.uid=voorkeuren.title group by leerlingen.firstname";

$studentArr = [];

while ($row = $result2->fetch_assoc()) {
    $studentName = $row['firstname'] . " " . $row['lastname'];
    if(!array_key_exists($studentName,$studentArr))
        $studentArr[] = $studentName;
     $studentArr[$studentName][] = $row['start'];
     $studentArr[$studentName][] = $row['end'];
}

foreach($studentArr as $key => $val){
    //your html code here
}
+1

, : ( )

:

SELECT CASE WHEN t.id = (SELECT t2.id
FROM distance t2 WHERE t2.fromid = t.fromid
GROUP BY t2.id LIMIT 1)
THEN t.id
ELSE '' END AS FromId, t.toid, t.id, t.distance
FROM distance t

.. , , JOIN. . .

:

prevent_duplicate

0

Source: https://habr.com/ru/post/1664413/


All Articles