Bounty Hunter 23 Posted November 4, 2009 Share Posted November 4, 2009 I'm producing a list (table) of businesses from a database containing Business Name and Business Category (among other things). I want the category to print as a header, then all the businesses within that category, then the next header, etc. Here's what I have at this point: [connect to DB] $result = mysql_query("SELECT * FROM " . $tablename . " WHERE status = 'Active' order by BusinessCat, BusName"); $result_1 = mysql_query("SELECT * FROM " . $tablename . " WHERE status = 'Active' order by BusinessCat, BusName limit 0, 1"); echo '<table border="1"> <tr>// output table header here ..</tr> $row = mysql_fetch_array($result_1); $lastcat = "empty"; do { // Test for new Category? if ($row['BusinessCat'] != $lastcat) ; echo "<tr><td>" . $row['BusinessCat'] . " </td></tr>"; echo "<tr>"; echo "<td><b>" . $row['BusName'] . "</b></td>"; //.. output a bunch of other stuff .. echo "</tr>"; $lastcat = $row['BusinessCat']; } while($row = mysql_fetch_array($result)); echo "</table>"; I'm close. Is this the besty way to go at this problem? Other database systems make section heading easy. I tried tghe "Group" angle, but I'm not summing or doing any calculations on the data - just trying to print a section heading each time the category changes. For some reason there is a loop that prints blank records after the whiole loop terminates. I will continue to work on these, but I'm basically in a trial and error mode now. Here is the link to the actual report. http://www.granitefallswa.com/data/membershipg.php Quote Link to comment https://forums.phpfreaks.com/topic/180238-produce-table-with-section-headings-from-mysql-query/ Share on other sites More sharing options...
sasa Posted November 4, 2009 Share Posted November 4, 2009 change if ($row['BusinessCat'] != $lastcat) ; echo "<tr><td>" . $row['BusinessCat'] . " </td></tr>"; to if ($row['BusinessCat'] != $lastcat){ echo "<tr><td>" . $row['BusinessCat'] . " </td></tr>"; $lastCat= $row['BusinessCat'];} remove ; in if line Quote Link to comment https://forums.phpfreaks.com/topic/180238-produce-table-with-section-headings-from-mysql-query/#findComment-950899 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.