I was running a parent/child query using self joins and just realized a month later that my query doesn't actually work the way I need. I have a table called
ix_page
the parent id is held in the field below_page
i have a field called access which stores the access level for that page
I also have a field called include which determines whether or not that page should be included in the navigation. Value can be 'yes' or 'no' My original query looked like this:
SELECT t1.id AS lev1, t2.id as lev2, t3.id as lev3, t4.id as lev4,
t5.id as lev5, t6.id as lev6, t7.id as lev7,
t8.id as lev8, t9.id as lev9, t10.id as lev10
FROM $table AS t1
LEFT JOIN $table AS t2 ON t2.below_page = t1.id
LEFT JOIN $table AS t3 ON t3.below_page = t2.id
LEFT JOIN $table AS t4 ON t4.below_page = t3.id
LEFT JOIN $table AS t5 ON t5.below_page = t4.id
LEFT JOIN $table AS t6 ON t6.below_page = t5.id
LEFT JOIN $table AS t7 ON t7.below_page = t6.id
LEFT JOIN $table AS t8 ON t8.below_page = t7.id
LEFT JOIN $table AS t9 ON t9.below_page = t8.id
LEFT JOIN $table AS t10 ON t10.below_page = t9.id
WHERE t1.include='yes' ORDER BY t1.sort_weight DESC
It was returning the results to me as:
lev1
lev 1 > lev2
lev1 > lev2 > lev3
or using words:
index
company
company > history
company > careers
company > management
solutions
solutions > site_development
solutions > site_development > content_management
etc.
for each page, then I had a function that cleared empty levels (usually lev4 through 10 return null, but I need my program to have the ability to go down 10 levels deep).
The problem I'm running into is I need to check that include is set to yes for each level, where as right now it's only checking level 1. I tried adding additional clauses such as AND t2.include='yes' but then that excludes all pages that are not at least two levels deep. If I were to go as far as t3.include='yes' it would exclude all pages that are not at least three levels deep.
I also need to ensure that the access fits the right user, and i have a function that I slip into the query for this. The function returns something like:
access in('public', 'member')
but once again I need to test this on every level. How can I modify my query to ensure only the right pages are included?
I'm open to a completely different approach using this db structure if anyone has ideas, I would just really like the query to return in the format I have now as I have the rest of my project built around that.