mysql - How to pull grandchildren from database -


I want to draw menu items from MySQL. Main ID = 1, Parent = 0- Us ID = 2, Parent = 1 - Music ID = 3, Parented = 1 - Rock ID = 8, Parented = 3 - Classic ID = 9, Parented = 3 -Car ID = 4, Parented = 1 - Toyota ID = 5, Parented = 4, - Ford ID = 6, Parented = 4, --Haida ID = 7, Parented = 4 Other Menu ID = 10, parent = 0 - othermind id = 11, parent = 10 --submenu id = 12, parent = 11 etc.

I can extract data from id = 1 to 4 and display "... where parent = 1" etc. Although it only pulls the top level

But I also want to remove all the data including the submenu for each menu (main menu).

Can someone tell me how to write a query

Need to implement Recursio To retrieve all the children again and again, you will have to change your database architecture layer implementation on your own, but the concept is the same.

Memcache solution

  function generator ($ parent = 0, & amp; $ tree) {$ sql = sprintf ('Select from Parents =% d ', $ parent with excursion); $ Res = $ the- & gt; DB- & gt; Results ($ sql); If ($ res) {foreach ($ res as $ r) {// push current tree $ tree [$ r-> ID] = $ r; // Make placeholder for children $ tree [$ r-> Id] ['children'] = array (); // Find any child of the child currently found $ tree = generatree ($ r-> ID, $ tree [$ r-> ID] ['children']); }}}} Gettree ($ parentid) {// memcache implementation $ memcache = new Memcache (); $ Memcache- & gt; Connect ('local host', 11211) or die ("could not connect"); $ Tree = $ memcache- & gt; Receive ('navigation'. $ Parent); If you need to query for ($ tree == null) {// tree $ tree = array (); Income tree ($ parent, $ tree); Memcache // memcache- $ one- memcache-> Store in set ('navigation'. $ Parentid, $ result, 0, 3600); } $ Tree returned; } // Find tree with tree = 0 (0);  

Non-memo solution

  function generated tree ($ parentid = 0, and $ tree) {$ sql = sprintf (' Select from where the parents =% d ', $ parentID); $ Res = $ the- & gt; DB- & gt; Results ($ sql); If ($ res) {foreach ($ res as $ r) {// push current tree $ tree [$ r-> ID] = $ r; // Make placeholder for children $ tree [$ r-> Id] ['children'] = array (); // Find any child of the child currently found $ tree = generatree ($ r-> ID, $ tree [$ r-> ID] ['children']); }}} // gets a tree with a parent = 0 $ tree = array (); $ Parent = 0; Income tree ($ parent, $ tree); // Results of the results of your tree var_dump ($ tree); Die;  

The above testing has not been done, if anybody caught the error, please tell me or do not hesitate to update.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -