Hierarchies and MySQL with PHP

From: Date: Sun, 27 Jun 2004 16:58:30 +0000
Subject: Hierarchies and MySQL with PHP
Groups: php.general 
Request: Send a blank email to php-general+get-189118@lists.php.net to get a copy of this message
Hi, I wonder what you think are the best (or "least worst") strategies to store and retrieve hierarchial data (such as a "threaded" discussion or a multi-level menu tree) in MySQL using PHP? I have been using table structures where each row contains a parent reference, such as: Table Example:
Field name    data type/db flags                    Coments
=================================================================
RowID         int unsigned auto_increment not null  (primary key)
ParentRowID   int unsigned                          0 (or NULL if at top level)
Name          varchar(50)
... which is OK for *defining* the hierarchy. However, it's a pain to retrieve the data so that it can be displayed in a nice threaded/sorted way, where children are sorted directly below their parents. I also want the items to be nicely sorted within their own branch, of course. On MS SQL, I successfully used stored procedures that employ temporary tables and while statements and the like. That method is not available in MySQL (yet), so I'll have to do a lot of the manipulation on the web server instead, using PHP. Any suggestions? /Mattias

« previous php.general (#189118) next »