Jump to content

produce table with section headings from MYSQL query


Bounty Hunter 23

Recommended Posts

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

 

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

Archived

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

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