Jump to content

Selcetive List Loopping


Daguse

Recommended Posts

I am looking a for a way to echo out data from a DB in a loop. However I do not want to display repeat info. I will be pulling Dates. So if I have pulled say September 2005 two times in a row and November 2005 4 times in a row. I want it only to echo September only once and November only once. Can any one help me out.
Link to comment
https://forums.phpfreaks.com/topic/8602-selcetive-list-loopping/
Share on other sites

You can retrieve unique rows from a table by using SELECT DISTINCT column_name, or use the GROUP BY clause.

However, if you can't use those for what you need, then you have to at least sort the rows using ORDER BY. Then it's just a matter of determining when the data has changed.

Example:
[code]
// open, select DB, and query table using order by

$saved_value = '';   // Initialize to something that the data won't contain

while ($row = mysql_fetch_assoc($result)) {

    // Determine if data has changed and display one time (heading)
    if ($row['column_name'] != $saved_value) {
        echo 'Heading: ', $row['column_name'], '<br/>';
        $saved_value  = $row['column_name'];
    }

    // Display other column data
    echo $row['name'];

}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/8602-selcetive-list-loopping/#findComment-31557
Share on other sites


Select syntax from manual:
[a href=\"http://dev.mysql.com/doc/refman/4.1/en/select.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/4.1/en/select.html[/a]

Tutorial:
[a href=\"http://www.keithjbrown.co.uk/vworks/mysql/mysql_p3.shtml\" target=\"_blank\"]http://www.keithjbrown.co.uk/vworks/mysql/mysql_p3.shtml[/a]
Link to comment
https://forums.phpfreaks.com/topic/8602-selcetive-list-loopping/#findComment-32189
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.