hi,

wonder if i could have some input please on where im going wrong, spent a 
few hours getting my head round this, but can't seem to get it right.

taking inspiration from this: 
http://wizardinternetsolutions.com/web-database-design/single-query-dynamic-multi-level-menu/

I am trying to build up a array of elements that replicates the static 
method in menus.py:

response.ttt = db((db.page.showinmenu==1)&(db.page.active==1)).select(db.
page.id, db.page.menutext, db.page.pageurl, db.page.parent, orderby=db.page.
parent|db.page.sort|db.page.menutext)
response.tttmenu = [[],dict()]
for subpage in response.ttt:
    response.tttmenu[0].append({subpage.id: [subpage.menutext, subpage.
pageurl, subpage.parent]})
    if subpage.parent in response.tttmenu[1]:
        response.tttmenu[1][subpage.parent].append(subpage.id)
    else:
        response.tttmenu[1][subpage.parent] = [subpage.id]

def buildmenu(parent, menu):
    html = ''
    if menu[1][parent]:
        html += '<ul>'
        for itemid in menu[1][parent]:
            if itemid in menu[1]:
                html += str(menu[0][itemid])
                #html += '<li>'+str(menu[0][itemid])
                #html += buildmenu(menu[0][itemid][1][0], menu)
                html += '</li>'
            else:
                #html += '<li>'+str(menu[0][itemid])+'</li>'
                html += 'Badgers'
    html += '</ul>'
    return html

response.tttnewmenu = buildmenu(0, response.tttmenu)

so the first bit sticks the query results into a array, the page elements 
into array index 0 as arrays and then the list of parents into array index 
1 as dictionaries for each element that has parents.
tttmenu outputs:

[[{1: ['Home', 'home', 0]}, {3: ['page3', 'page3', 0]}, {2: ['page2', 
'page2', 1]}], {0: [1, 3], 1: [2]}]

which seems right compared to the link above, but i just can't get the 
second bit right.

that is supposed to query through the parents dictionary keys and for each 
array element inside the key, check if it is a parent and if so go through 
the function again, so that it puts order into the output to the same 
standard as the static response.menu example. the intention is to replace 
all the html in the function and turn it into an array, but wanted to walk 
through and get the code right.
The main problem i've got is calling the page array element 0 (the page 
title) based on the dictionary key element that is being looped, just can't 
get the syntax right no matter how much i try.

any help would be greatly appreciated, even if you just tell me im doing it 
wrong and should look at something else.

was looking at doing things this way to first prevent too many database 
queries on menu building and also to save having an additional table in the 
database dedicated to the menu.

all part of a cms im making and intend to release once it's any good.

Thank you,

Alan


Reply via email to