php - How can I recursively search through a database? -
i have mysql table 3 columns *id, item_id, dependency*
i want know best way recursively check through dependencies example checking item_id 3's dependencies , requires item_id 2 need check dependencies item_id 2
what best way achieve php?
id | item_id | dependency | ---+---------+------------+ 1 | 3 | 2 | ---+---------+------------+ 2 | 2 | 4 | ---+---------+------------+ 3 | 2 | 5 | ---+---------+------------+
pseudo code:
func myquery(item_id) { query (item_id) if (!item_id.dependency()) else (myquery(item_id.dependency()) }
this continue down column until reach item not have dependency.
summary of process: pass first item function. check if has dependency, if does, call function again dependency. otherwise nothing , function return. of course you'll want save items returned query i'll leave you. want statement @ end of function when said , done have data ordered no dependencies dependencies.
Comments
Post a Comment