troubleshooting Question

Looping/Sorting Question PHP/MySQL

Avatar of BongSoo
BongSooFlag for United States of America asked on
PHP
13 Comments1 Solution234 ViewsLast Modified:
Using PHP/MySQL, I  have a query

// Query database to get all sources that match search criteria
$res_search = mysql_query("SELECT showID, bandname, date, source, notes FROM music WHERE ('$SearchBandName' = bandname) OR ('$SearchShowDate' = date) OR ('$SearchSource' = source) ORDER BY date") or die("SQL error: " . mysql_error());

When I search by source, it can return results from multiple bands. Right now, I have it set up so that it loops through and lists each show including the band's name:

            while ($row = mysql_fetch_array($res_search, MYSQL_BOTH)) {            
            
            echo '<p><strong>';
            echo $count;
            echo ')&nbsp;';
            $count++ ;
            echo $row[1]; // Band name
            echo '</strong><br />';
            echo $row[2];  // Date
            echo '<br />';
            echo $row[3];  // Source
            echo '<br />';
            echo $row[4]; // Notes
            echo '</p>';
            }

Which might return results like this:

1) Grateful Dead
1966-07-16
Audience

2) Grateful Dead
1968-01-22
Audience

3) Grateful Dead
1968-03-03
Audience

4) Phil Lesh & Friends
1994-09-24
Audience

5) Phil Lesh & Friends
1998-04-20
Audience

6) Phil Lesh & Friends
1999-07-02
Audience

But, what I really want would to have it list the band name as a heading, and then list that band's shows, and then the next band name, and then so on like this:

Grateful Dead

1) 1966-07-16
Audience

2) 1968-01-22
Audience

3) 1968-03-03
Audience

Phil Lesh & Friends

4) 1994-09-24
Audience

5) 1998-04-20
Audience

6) 1999-07-02
Audience

My attempts at creating a loop to do this have fallen flat. Hopefully this is a quick and easy sort?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 13 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros