I’ve a table, basically its something like
------------------
id | name | type
------------------
1 | name1 | type1
2 | name2 | type2
3 | name3 | type3
------------------
I would like to query it and display it into something like
type1
- name1
- and so on...
type2
- name2
- and so on...
type3
- name 3
- and so on...
I am also looking to display it as a JSON file which is something like
[
{
"type1": {
"name": "name1"
},
"type2": {
"name": "name2"
},
"type3": {
"name": "name3"
}
}
]
So, may I know the best way to do it?
Using loops to query the type, then selecting the categories and displaying it by type?
Edit :
After searching high and low in the internet. I’ve found this : http://www.tommylacroix.com/2008/09/10/php-design-pattern-building-a-tree/
Hence formulated this code for my needs. Not sure if it’s efficient :
<?php
$query = "SELECT * FROM TABLE";
$list = array();
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
if(!$list[$row['type']])
{
$list[$row['type']] = array();
}
array_push($list[$location['type']],&$row['name']) ;
}
?>
Try This…. also see the result output below
Result Output