* hengky <[EMAIL PROTECTED]> [2005-02-13T19:05:25]
>  ---------------------------------------
> | id_cat  | nama_category   | id_parent |
>  --------- ----------------- -----------
> | 001     | Computer        | TLCAT     |
> | 002     | Motherboard     | 001       |

Why "TLCAT" and not NULL?  Then you could use a proper numeric type.

> while(@DataCat = $sth->fetchrow_array() ) {
>     [...]
>     ONE:
>     while() {
>       [...]
>       while(@DataCat2 = $sth2->fetchrow_array() ) {
>       [...]
>      }
>    }
>  }

Not pictured: "last ONE:"  You don't need a colon there.  The colon goes
where the label is declared, not when you mention it.  "last ONE;" would
have worked.

That said, what you want here is a recursive sub.

  sub full_name_for {
    my ($id) = @_;
    my ($name, $parent) = $dbh->selectrow_array(
      "SELECT name, parent FROM categories WHERE id = ?",
      undef
      $id
    );
    return $parent ? $name : (full_name_for($parent) . "/$name")
  }

So: given an id of a category:
  1. looks up its name and parent
  2. if it has no parent, return the name
  3. if it has a parent, return the parent's name followed by the
     category name
  4. to determine the parent's name, go to step one

Eventually, if you have no loops, you will get to a top level element
and stop recursing.  I will leave loop detection up to you.

-- 
rjbs

Attachment: pgpjbApbHjdA1.pgp
Description: PGP signature

Reply via email to