Forum Moderators: coopster

Message Too Old, No Replies

Showing lists by state

         

Hollywood21

6:43 pm on Sep 17, 2007 (gmt 0)

10+ Year Member



I need to pull out a large list of companies out of a database for display on the web. They are listed by state, and I can sort them by state, but I need to show the state name as a heading above each state's list. How do I accomplish that?

coopster

7:04 pm on Sep 17, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, Hollywood21.

Well, your SELECT statement might look something like this ...

SELECT stateName, companyName FROM myTable ORDER BY stateName, companyName

... and then you will fetch your rows from the result set and every time the stateName changes you can print out a header using the value in that column.

Hollywood21

7:09 pm on Sep 17, 2007 (gmt 0)

10+ Year Member



That works. Next I need to add a heading above each state though -- for example:

California

Companyname 1
Companyname 2
Companyname 3

coopster

7:37 pm on Sep 17, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yep, so while you are looping through your result set, compare the value in the fetched row with a "holder" field that contains the last value you read in. If it is different, print out your header. Some pseudocode to demonstrate ...
$stateName = false; // initialize 
while ($row = fetching rows from database) {
if ($row['stateName']!== $stateName) {
$stateName = $row['stateName'];
print '<h2>' . htmlentities($stateName) . '</h2>';
}
// print out your company stuff
}

Hollywood21

7:53 pm on Sep 17, 2007 (gmt 0)

10+ Year Member



That did the trick! Thank you!