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

 

Link to comment
Share on other sites

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

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.