You have a typical hierarchical data issue, can be solved with a graph.
Theory review: http://www.algorithmist.com/index.php/Graph_data_structures

Mysql and PHP implementation:
http://www.phpro.org/tutorials/Managing-Hierarchical-Data-with-PHP-and-MySQL.html

Reply via email to