Jump to content

Archived

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

Daguse

Selcetive List Loopping

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.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Thank you I will read up on my MySQL querys and try the SELECT DISTINCT column_name, or the GROUP BY clause.

Share this post


Link to post
Share on other sites
Ok I need you guys help. I can't seem to find the syntax for SELECT DISTINCT and GROUP BY.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.