使用类别在PHP中显示MySQL数据

I have a table that looks like this:

+----------------------------------+
|    Category   |    Sub-Category  |
+---------------+------------------+
|  Cell Phones  |   Smart Phones   |
|  Cell Phones  |   Tablet Phones  |
|  Cell Phones  |    Other Phones  |
|   Computers   |      Desktops    |
|   Computers   |      Laptops     |
|   Computers   |    Chromebooks   |
+---------------+------------------+ etc..

And I would like to display this data on my website using PHP, like this:

desired layout

How would I display the information as above(one category, all subcategories under) using PHP? Also how can I evenly divide the list of category/subcategories into 3 columns?

I've found tutorials but none seemed to use PHP, which is the part I'm having trouble with.

Thank you guys for your time.

$data = query_data();
$cat = array();
foreach($data as $item)
{
    $cat[$item['category']][] = $item['sub_category'];
}

Now, $cat is what you need.

This type of reports are called cross tabing reports. In sql there is pivot function which gives data as you require. But in mysql there is no pivoting so you need to use your own logic.
To achieve this you will need to have two loops as described below a short idea on how to do this.

Select distinct maincategory from table
Loop for main category{
  select subcategory from table where maincategory='category'{
   print all sub categories
  }
}

You can give category id to them for example, say cellphones has an id of 1 then all items inside this will also have a subcategory_id of 1. Similarly if the printers have id of 2 then all subcategory_id under it will have id as 2.

Now you can run simple query to retrieve the field specific category. like:

SELECT * from table_name WHERE subcategory_id = 1;

This will give out all the categories inside cellphones...similarly you can do for all fields...

This should work -

$categories = Array();
//Assuming database handle is $db
$r1 = $db->query("select distinct(Category) from table_name") or die("err in q1");
while($cat_arr = $r1->fetch_assoc()){
    $cat = $cat_arr['Category'];
    $sub_cats = Array();
    $db->query("select Sub-Category from table_name where Category = $cat") or die("err in q2");
    while($sub_arr = $r2->fetch_assoc()){
        array_push($sub_cats,$sub_arr['Sub-Category']);
    }
    $categories[$cat] = $sub_cats;
}

The $categories array now has the details of the table, and can be used as is.