greyhat1123 0 Newbie Poster

Good evening:

I need assistance getting data formatted properly when it is pulled from my database. The database pulls everything correctly, so let's assume the following table:


$results = $db->query($sql);

+----+--------------+--------------+-----------+
| id | name_of_item | cost_of_item | item_type |
+----+--------------+--------------+-----------+
| 1  | product01    | 20           | 1         |
+----+--------------+--------------+-----------+
| 2  | product01    | 30           | 1         |
+----+--------------+--------------+-----------+
| 3  | product02    | 10           | 2         |
+----+--------------+--------------+-----------+
| 4  | product02    | 40           | 2         |
+----+--------------+--------------+-----------+

Assuming the above is the results, I would like to provide the option to output the information in various formats. Ideally, I want a table output that would look like:

+----------------------------------------------+
| product01                                    |
+----------------------------------------------+
| cost       |  item_type                      |
+----------------------------------------------+
| cost       |  item_type                      |
+----------------------------------------------+
| product02                                    |
+----------------------------------------------+
| cost       |  item_type                      |
+----------------------------------------------+
| cost       |  item_type                      |
+----------------------------------------------+

Or like:

+----------------------------------------------+
| item_type                                    |
+----------------------------------------------+
| cost       |  product_name                   |
+----------------------------------------------+
| cost       |  product_name                   |
+----------------------------------------------+
| item_type                                    |
+----------------------------------------------+
| cost       |  product_name                   |
+----------------------------------------------+
| cost       |  product_name                   |
+----------------------------------------------+

I'd like to possibly have this be flexible so I can readily change the output headers and sort order--so I could view by item_type or cost or product_name.

How can this be done in PHP?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.