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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.