
Please consider joining the PHP-Fusion 8 Team by submitting this form below.
| [A] Archer | 00:18:03 |
| [SA] Richard Ainz | 01:42:46 |
| [A] Yodix | 02:16:49 |
| [A] hien | 04:03:54 |
| [SA] Halisson Ricardo | 07:48:21 |
» Show More Admins «
| [A] jikaka | 10:31:10 |
| [SA] Ankur | 11:16:09 |
| [SA] Domi | 14:12:45 |
| [SA] JoiNNN | 1 Day |
| [A] WolfCore | 1 Day |
| [A] Arda | 4 Days |
| [A] David | 4 Days |
| [A] NetriX | 6 Days |
| [A] FILON | 1 Week |
| [A] MeTRoiD | 1 Week |
| [A] Matthew | 3 Weeks |
| [A] kneekoo | 3 Weeks |
| [A] Christian | 4 Weeks |
| [A] KEFF | 4 Weeks |
| [A] lelebart | 5 Weeks |
| [A] samem | 6 Weeks |
| [SA] HobbyMan | 11 Weeks |
| [A] Matonor | 16 Weeks |
| [U] Rolly8 HL php | ![]() |
| [U] Craig | 00:29:06 |
| [U] Lippke | 00:40:43 |
| [U] khaman | 00:42:46 |
| [U] Sony | 02:00:19 |
» Show More Users «
| [U] PolarFox | 02:41:50 |
| [U] djdandi | 03:01:51 |
| [U] Korcsii | 03:15:37 |
| [U] ctokepa | 04:11:53 |
| [U] byte | 11:29:01 |
| [U] Wanabo | 15:31:32 |
| [U] krystian1988 | 1 Day |
| [U] Johnny Viinblad Jensen | 1 Day |
| [U] KasteR | 1 Day |
| [U] Leina | 2 Days |
I learned it from sitepoint.com
Basically, there are two approaches. One is much more simple than the other.
Parent - Child relation for categories.
Lets say you have a set of categories identified by category_id and parent_id, each having its own column in a table:
cat_id, par_id, cat_name
1, 0, Hats
2, 0, Shoes
3, 0, Coats
4, 1, Baseball Hats
5, 1, Fedoras
6, 2, Tennis Shoes
7, 2, Formal Shoes
8, 3, Winter Coats
9, 3, Formal Coats
You would want them to show up in a tree like so:
Hats
-Baseball hats
-Fedoras
Shoes
-Tennis Shoes
-Formal Shoes
Coats
-Winter Coats
-Formal Coats
How to achieve this is, you make a recursive function that looks up all of the categories with a parent of 0, then find all of the children of that, then conditionally call the function within itself. Using this, you can have an unlimited depth of sub-categories.
I can provide code for the above as an example if needed (from an infusion i'm developing)
The next way to do it is by the left-to-right method, which is significantly more complicated but takes less server processing time. It is explained further in the article at sitepoint.com
Table _article_cats
Id | name | category
1 parent 0
2. Child 1
Load the parent in function
Function callchild ($ parent_id, $table_name, $ callback) {
$ result = dbquery("select * from $ table_name where category='$ parent_id'"
While ($ data=dbarray ($ result)) {
// callback 1 ouputs value format
If ($ callback == 1) { $output .= $ data ['id']; }
// callback 2 outputs array format
If ($ callback == 2) { $ output = array_push ($ data ['id']) }
}
}
Need this?