So, I have a database table that has up to 8 separate category options for each customer.
Example:
company_name | category_1 | category_2 | category_3
****************************************************
My Company | computers | parts | electronics
ect... up on up to eight category options. What I need to do is get the categories in a list and list all companies with that category under each category item. I have the categories into an array, but I get all of them in a foreach loop which will give me duplicates. I don't want to list the duplicates, I just want to list them once and place all companies under that category.
Like:
Computers
Company Name
Parts
Company Name
Electronics
Company Name
ect....
My code currently:
$sql = $wpdb->get_results( "SELECT * FROM $table_name");
echo '<ul>';
foreach ($sql as $cat){
$cats[0] = $cat->category_1.' '.$cat->category_2.' '.$cat->category_3.' '.$cat->category_4.' '.$cat->category_5.' '.$cat->category_6.' '.$cat->category_7.' '.$cat->category_8;
$totalCats = $cats[0];
echo '<li>'.$totalCats.'</li>';
}
echo '</ul>';
}// End of foreach loop
This will then give me the following:
- Computers Parts
- Computers Electronics
- Electronics Parts
ect... for each database entry depending on how many categories that company chose.
Any help would be appreciated!