1

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.

2 Answers 2

1

I don't have a dataset to test it but this should work. We create a multidimensional array from the dataset like this:

[comp][0] = company A
      [1] = company B
      ...
[elec][0] = company A
      [1] = company C
      ...
[part][0] = company Y
      [1] = company Z
      ...

Then we iterate over it to print it out.

<?php
$cats = array();

// loop through rows
foreach($sql as $cat) {

   // loop through row categories
   for($i=1; $i<=8; ++$i) {

      // column name
      $column = 'category_'.$i;

      // column has data
      // $cats[category][irrelevant index] = company name
      if($cat->$column !== null && $cat->$column !== '') {
         $cats[$cat->$column][] = $cat->company_name;
      }
   }
}

// sort categories
ksort($cats);

echo '<ul>';

// loop though categories
foreach($cats as $catname=>$cat) {

   // sort companies in category
   sort($cat);

   // category name
   echo '<li>'.$catname.'</li><ul>';

   // loop through companies
   foreach($cat as $company) {
      echo '<li>'.$company.'</li>';
   }

   echo '</ul>';
}
echo '</ul>';
?>

It would probably be best to store categories in a separate table and start from there, using a JOIN to retrieve associated company names. It would also require less code to print it out the way you want it.

update 2015-07-09 10:02 +0000

Changed column data testing to $cat->$column !== ''

update 2015-07-09 10:49 +0000

Changed column data testing to $cat->$column !== null && $cat->$column !== '' based on wpdb results structure.

Sign up to request clarification or add additional context in comments.

5 Comments

That's almost right on the money! I tried that and I got the categories listed out with all the companies under them, but above that I get the companies listed as many times as they are in categories. See example: pastebin.com/Wzxvwduw
Can you add print_r($cats); at the end of my code and post its output ?
Exactly what I suspected. Just need to run some tests to see how to fix it.
Thanks for the update, however it is still showing up. pastebin.com/gHs1XWxH
1

I think you need to create separate table for categories

For eg:-

table companies

company_id | company_name

table categories

category_id | company_id | category_name

So your query will comes like

$query = "SELECT * FROM companies comp RIGHT JOIN categories cate ON comp.company_id=cate.company_id";

With this you will get company details and array of categories, hope this is the better way to do this

1 Comment

I would suggest using 3 tables: "companies" (id, name), "categories" (id, name), "companies_categories" (company_id, category_id).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.