Jump to content


Photo

Selcetive List Loopping


  • Please log in to reply
4 replies to this topic

#1 Daguse

Daguse
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 28 April 2006 - 03:58 AM

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.

#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 28 April 2006 - 04:21 AM

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:
// 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'];

}


#3 Daguse

Daguse
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 28 April 2006 - 05:22 PM

Thank you I will read up on my MySQL querys and try the SELECT DISTINCT column_name, or the GROUP BY clause.

#4 Daguse

Daguse
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 30 April 2006 - 05:18 AM

Ok I need you guys help. I can't seem to find the syntax for SELECT DISTINCT and GROUP BY.

#5 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 30 April 2006 - 04:23 PM


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]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users