How can you efficiently display multiple rows from a database while only showing certain fields once in PHP?

When displaying multiple rows from a database in PHP, you can efficiently show certain fields only once by using a flag variable to keep track of unique values. You can loop through the rows and check if the current field value is the same as the previous one. If it is different, display the field value, otherwise skip displaying it.

<?php
// Assuming $rows is an array of data fetched from the database

$flag = ''; // Initialize flag variable

foreach ($rows as $row) {
    if ($row['field'] != $flag) {
        echo $row['field'] . '<br>'; // Display the field value
        $flag = $row['field']; // Update flag variable
    }
    // Display other fields as needed
}
?>