I assume you are trying to build a hierarchial tree of articles and
you want them all from the parent.

I would do this something like this:

db.define_table('article',Field('f_parent','reference
article'),Field('title'))


def index():
    rows = dict((r.id,r) for r in db(db.article).select())
    for id,row in rows.items(): row.children=[]
    for id,row in rows.items():
        if row.f_parent==0: root=row
        else: rows[row.f_parent].append(row)
    def tree(row):
        return DIV(H1(row.title),UL(*[LI(tree(child)) for child in
row.children])))
    return tree(root)


On Nov 28, 5:00 pm, JmiXIII <[email protected]> wrote:
> Hello,
>
> I'm facing I guess a well known programming problem. It is not really
> related to web2by, but since I'm using web2py I hope somebody here can
> help.
>
> Here is my controller :
>
> def global_view():
>     parents = db(~(db.t_article.id==db.t_list.f_article)).select()
>     tree={}
>     for parent in parents:
>         tree[parent.t_article.id]={}
>         sublevel=db(parent.t_article.id==db.t_list.f_parent).select()
>         for son in sublevel:
>             tree[parent.t_article.id][son.f_article]=son.f_article
>             tree[parent.t_article.id][son.f_article]={}
>             sublevel2=db(son.f_article==db.t_list.f_parent).select()
>             for son2 in sublevel2:
>                 tree[parent.t_article.id][son.f_article]
> [son2.f_article]=son2.f_article
>                 tree[parent.t_article.id][son.f_article][son2.f_article]={}
>     return dict(tree=tree)
>
> So in fact I'd like to go on for son3, son4, based on the same model
> as sublevel2... until sublevelx=None.
> I guess I should use some kind of recursive or loop function.
> I've read different things using right and left variable for
> hierarchical tree, but it seems to me that it is possible to build a
> loop in my controller without modifying my model.
>
> Hope I do not disturb this groupes with this kind of question.

Reply via email to