lauralee Posted February 16, 2010 Share Posted February 16, 2010 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 More sharing options...
ShadowMage Posted February 16, 2010 Share Posted February 16, 2010 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 More sharing options...
lauralee Posted February 16, 2010 Author Share Posted February 16, 2010 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 More sharing options...
justsomeguy Posted February 16, 2010 Share Posted February 16, 2010 Something before that line is causing the error, there's nothing wrong with that line itself. Link to comment Share on other sites More sharing options...
ShadowMage Posted February 16, 2010 Share Posted February 16, 2010 Oops that's my bad. I missed a ';' on this line:$lastCategory = $row['category_name']Just put the semicolon at the end and your error should go away. Link to comment Share on other sites More sharing options...
lauralee Posted February 16, 2010 Author Share Posted February 16, 2010 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 More sharing options...
ShadowMage Posted February 16, 2010 Share Posted February 16, 2010 Make sure that the line where you echo the business names is not in any of the if statements. It should be directly in the while loop. Link to comment Share on other sites More sharing options...
lauralee Posted February 16, 2010 Author Share Posted February 16, 2010 Thanks! It Works! Now I'll have to dissect the code you provided so I can understand what the heck it does. Much appreciation! Link to comment Share on other sites More sharing options...
ShadowMage Posted February 17, 2010 Share Posted February 17, 2010 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 More sharing options...
lauralee Posted March 3, 2010 Author Share Posted March 3, 2010 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 More sharing options...
ShadowMage Posted March 3, 2010 Share Posted March 3, 2010 Do any of the businesses under ACCOUNTANTS print?Maybe post your code so I can look at what you have. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.