Hi, I have a problem trying to sort out the side navigation to a script i'm making. It's still in the planning phase at the moment so I've put together the following examples to make this easier to explain.
Say I have the following table:
Categories:
- id (primary key)
- depth (the depth of the category)
- lineage (lineage containing the category path)
With the following data:
1 0 /1/
2 0 /2/
3 0 /3/
4 1 /3/4/
5 2 /3/4/5/
6 2 /3/4/6
7 1 /3/7/
8 2 /3/7/8/
9 2 /3/7/9/
10 3 /3/7/9/10
11 3 /3/7/8/10
Now If I was on the page where id = 3 you would get:
1
2
3
__4
__7
If I was on the page where id = 4 you would get:
1
2
3
__4
____5
____6
__7
If I was on the page where id = 10 you would get:
1
2
3
__4
__7
____8
____9
______10
I hope you see the pattern now. The problem i'm having is constructing the loop. I only want to display the categories in the structure above so I loop through each category using foreach and then use an if statement to get the categories I wish to display. Here's what I have put together so far:
I know it needs something else. So far I have said if the depth = 0 (top level category) i always want to display it. Secondly I make sure that category gets displayed if it is within one depth of the current category. Finally if the category id is 4 (as shown in one of the examples above) it would give:
1
2
3
__4
____5
____6
__7
____8
____9
So the if statement needs something extra to restrict the display of categories it does not contain.
I've been trying to rack my brains around this for the last few hours and it has exhausted me.
Would appreciate it if someone could help. Thanks
Say I have the following table:
Categories:
- id (primary key)
- depth (the depth of the category)
- lineage (lineage containing the category path)
With the following data:
1 0 /1/
2 0 /2/
3 0 /3/
4 1 /3/4/
5 2 /3/4/5/
6 2 /3/4/6
7 1 /3/7/
8 2 /3/7/8/
9 2 /3/7/9/
10 3 /3/7/9/10
11 3 /3/7/8/10
Now If I was on the page where id = 3 you would get:
1
2
3
__4
__7
If I was on the page where id = 4 you would get:
1
2
3
__4
____5
____6
__7
If I was on the page where id = 10 you would get:
1
2
3
__4
__7
____8
____9
______10
I hope you see the pattern now. The problem i'm having is constructing the loop. I only want to display the categories in the structure above so I loop through each category using foreach and then use an if statement to get the categories I wish to display. Here's what I have put together so far:
Code:
foreach cats as cat {
if cat[depth] = 0 or (cat[depth] <= depth + 1 and ?????) {
// display cat
}
}
I know it needs something else. So far I have said if the depth = 0 (top level category) i always want to display it. Secondly I make sure that category gets displayed if it is within one depth of the current category. Finally if the category id is 4 (as shown in one of the examples above) it would give:
1
2
3
__4
____5
____6
__7
____8
____9
So the if statement needs something extra to restrict the display of categories it does not contain.
I've been trying to rack my brains around this for the last few hours and it has exhausted me.
Would appreciate it if someone could help. Thanks