Jump to content

Listing Variables Without Repeating Category Name


lauralee

Recommended Posts

I use a table containing category names for a select option in an entry form for another table that contains business information. I want to be able to show a list of those businesses under each particular category, without repeating the category name itself. By using a INNER JOIN, I can retrieve the information I need, but I haven't been able to display it like I want. I've tried using foreach, and while for the arrays with no luck. The category name always appears above the business name, not just once for all the businesses that belong to that category.<?php$sql = "SELECT * FROM Categories ORDER BY category_name";$result= @mysql_query ($sql); if (!result) { $error = 'Error fetching Business Name'; exit(); }$sql = "SELECT * FROM contacts INNER JOIN Categories ON category_name=category";$result=@mysql_query($sql);if (!result) { $error = 'Error fetching Join information'; exit(); } while ($row = @mysql_fetch_array($result)) { echo '<h4>' . $row['category_name'] . '</h4>';\\ HERE IS WHERE I NEED TO LIST THE BUSINESSES THAT BELONG TO EACH CATEGORY WITHOUT REPEATING THE \\CATEGORY NAME ABOVE EACH BUSINESS NAME echo '<p>' . $row['BusinessName'] . '</p>'; } ?>

Link to comment
Share on other sites

What I do in a situation like this is track the category name and only if it changes print out the name:

$blnInit = true;while ($row = @mysql_fetch_array($result)){if ($blnInit) {$lastCategory = $row['category_name']$blnInit = false;}$currCategory = $row['category_name'];if ($currCategory != $lastCategory) {echo '<h4>' . $row['category_name'] . '</h4>';}\\ HERE IS WHERE I NEED TO LIST THE BUSINESSES THAT BELONG TO EACH CATEGORY WITHOUT REPEATING THE \\CATEGORY NAME ABOVE EACH BUSINESS NAMEecho '<p>' . $row['BusinessName'] . '</p>';$lastCategory = $currCategory;}

Link to comment
Share on other sites

I copied and pasted the code you suggested, but I get an error on this line:$blnInit = false;the error is:Parse error: syntax error, unexpected T_VARIABLE in /home/chamber/public_html/Business/typeofbusiness.inc.html.php on line 28do I need to enclose that line with brackets or parens?

Link to comment
Share on other sites

Thanks. I should have seen the missing ";" too. I made the change, but the page displays only the category names, none of the businesses in that category. What am I missing?

Link to comment
Share on other sites

Thanks! It Works! Now I'll have to dissect the code you provided so I can understand what the heck it does. Much appreciation!
It's quite simple really. The variable $blnInit is used to detect the first time through the loop. It's initialized as true so when the loop runs through the first time it will perform a set of statements and then set $blnInit = false so that those statements don't get executed again:
$blnInit = true;while ($row = @mysql_fetch_array($result)){if ($blnInit) {$lastCategory = $row['category_name']$blnInit = false;}

$lastCategory stores the category of the previous row. Unless this is the first time through the loop, then it equals the first category in your result set.Every time the loop is run you need to set the $currCategory so you know what category you're currently on.

$currCategory = $row['category_name'];

When the category changes (when $currCategory is no longer equal to $lastCategory) you print the category header:

if ($currCategory != $lastCategory) {echo '<h4>' . $row['category_name'] . '</h4>';}

Then every time through the loop you need to print out the business names:

echo '<p>' . $row['BusinessName'] . '</p>';

And finally set $lastCategory equal to $currCategory so that it knows which category was done last and can compare that to the one its currently on.

$lastCategory = $currCategory;}

Also I just noticed something, you may notice that the first category probably doesn't print. This is because the first time through the loop the $currCategory and the $lastCategory are equal so the following is not true:

if ($currCategory != $lastCategory) {

To fix that just echo the category inside the if ($blnInit) statement:

if ($blnInit) {$lastCategory = $row['category_name']echo '<h4>' . $row['category_name'] . '</h4>';$blnInit = false;}

Link to comment
Share on other sites

  • 2 weeks later...

Thanks for explaining how the code works. And I did notice that the first category didn't show, and, I have attempted to get the first category, which is ACCOUNTANTS, to appear with your suggestion, but it won't. The second category, which is ADVERTISING, appears with the businesses in that category listed under it, but the accountants won't display. Any other suggestions?

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...