Nested Menus

Victor Subervi victorsubervi at gmail.com
Thu Oct 8 11:38:02 EDT 2009


Hi;
I have the following code:
  sql = 'create table if not exists categories (ID int(3) unsigned primary
key, Category varchar(40), Parent varchar(40))'
  cursor.execute(sql)
  cursor.execute('select Category, Parent from categories;')
  data = cursor.fetchall()
  parents = []
  Parents = []
  for dat in data:
    if dat[1] not in parents:
      parents.append(dat[1])
    Categories.append(dat[0])
    Parents.append(dat[1])
  Parents.sort()
  families = []
  for parent in Parents:
    children = []
    for dat in data:
      if dat[0] == parent:
        children.append(dat[1])
    families.append([parent, children])

Now, what I want to do is to capture all nested categories, such that if x
is the parent of y who is the parent of z, etc., I can print out the entire
family tree. I'm at a loss as to how to do it! Please advise.
TIA,
Victor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091008/4c3b872c/attachment.html>


More information about the Python-list mailing list