dongqiao1888 2014-01-23 07:36
浏览 55

使用类别在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.

  • 写回答

4条回答 默认 最新

  • duanpa2143 2014-01-23 07:41
    关注
    $data = query_data();
    $cat = array();
    foreach($data as $item)
    {
        $cat[$item['category']][] = $item['sub_category'];
    }
    

    Now, $cat is what you need.

    评论

报告相同问题?