PHP MySQL display multiple rows grouped by common fields -
i'm trying figure out how list company 's category 's , brand 's, layout similar this:
company 1
category 1:
brand x
brand y
brand zcategory 2
brand a
brand b
brand ccategory 3
brand a
brand x
i'm not familiar enough php + mysql find right search , php output in order achieve this.
my table looks similar this:
company | category | brand -------------------------------- company 1 | category 2 | brand company 1 | category 2 | brand b company 1 | category 2 | brand c company 1 | category 1 | brand x company 1 | category 1 | brand y company 1 | category 1 | brand z company 1 | category 3 | brand company 1 | category 3 | brand x
<?php $result = mysql_query(" select * some_table order company, category, brand ") or trigger_error('query failed in '. __file__ . ' on line '. __line__ .'. '. mysql_error(), e_user_error); if (mysql_num_rows($result)) { $companies = array(); while ($row = mysql_fetch_assoc($result)) { $companies[$row['company']][$row['category']][] = $row['brand']; } foreach ($companies $company => $categories) { echo '<h2>'. htmlentities($company, ent_compat, 'utf-8') .'</h2>'; echo '<ul>'; foreach ($categories $category => $brands) { echo '<li>'. htmlentities($category, ent_compat, 'utf-8'); foreach ($brands $brand) { echo '<br><em>'. htmlentities($brand, ent_compat, 'utf-8') .'</em>'; } echo '<br> </li>'; } echo '</ul>'; } }
Comments
Post a Comment