Ztree + PHP Infinite pole node Recursive lookup node Method _php Tutorial

Source: Internet
Author: User
Tags vars

Ztree + PHP Infinite pole node recursive lookup node method


First, preface

A simple description, the practice of a few principles, thinking, actually write a lot of things, thought is the most important.

1, Target: Will write an infinite node tree directory structure, such as

Steps:

1, your download plugin ztree. And then decorate it in your project.

 
  
  

2. Related CSS

 
  
  
  1. "stylesheet" href= "__public__/css/ztreestyle/ztreestyle.css" type= "Text/css"
  2. "stylesheet" href="__public__/css/ztree.css " type=" Text/css "

The above CSS and JS to your own prevail.

3. Directory Structure div

 
  
  
  1. "Content_wrap" style="background: #666;"
  2. "Ztreedemobackground Left"
    • "Treedemo" class= "Ztree" >
  3. "Content-text" id= "Text"

4, own the code in the JS alone

 
  
  

The relevant functions and configuration written inside!

 
 
  1. //Configuration Items
  2. var setting = {
  3. Issimpledata:true, //Data used in simple Array format, default false
  4. Showline:true , //whether the connection between nodes is displayed
  5. Checkable:true,
  6. Callback: {
  7. Onclick:ztreeonclick
  8. }
  9. };
  10. var Znodes; //Data variables
  11. //ajax submit data, request background PHP processing to return the directory structure JSON data
  12. $.ajax ({
  13. URL:"/admin.php/ztree",
  14. Type: "get",
  15. Async:false,
  16. DataType:"JSON",
  17. Success: function (data) {
  18. //alert (data);
  19. Znodes=data; //Save the data returned by the request.
  20. //alert (znodes);
  21. },
  22. Error: function () {//request failed handler
  23. Alert (' request failed ');
  24. },
  25. })
  26. //Initialize the ZTREE directory structure view!
  27. $ (document). Ready (function() {
  28. //alert ("111");
  29. $.fn.ztree.init ($ ("#treeDemo"), setting, znodes);
  30. });

5, the background php recursive algorithm, from the database to find the directory structure and generate JSON data

Address: As in 4, Ajax requested/admin.php/ztree "I am here to use the thinkphp framework, so the URL is like this, to your own interface file as the subject!"

 
 
  1. //Parent node Array
  2. $arr = Array ();
  3. $arr _str0 = Array ("name" = "function library query ',' children '="$this ->selectson (1)); //parent node pid=1;
  4. $arr _str1 = Array ("name" =' database query',' children '= $this->selectson (2)); //parent node pid=2;
  5. Array_push ($arr, $arr _str0);
  6. Array_push ($arr, $arr _str1); //This is a 2 parent node.
  7. Echo (Json_encode ($arr)); //This is the return data JSON data that was returned to the page, which is returned to the AJAX request
  8. ? >
  9. //Here is just a method, a call to the Selectson () method that returns an array of collections! But it uses recursion!
  10. //Find child node pid= parent Node ID
  11. Private function selectson ($Pid) {
  12. $m =m (' ztree ');
  13. if ($info=$m->where ("pid= ' $Pid '") Select ())) //Find the child ID under the parent ID
  14. {
  15. $data = Array ();
  16. for ($i=0; $i < Count ($info); $i ++)
  17. {
  18. $da = Array ("name" =$info[$i] [' name '] ,' children '=$this->selectson ($info[ $i[' id '])); //recursive algorithm!
  19. Array_push ($data, $da); //Join a child node array
  20. };
  21. return $data; //Returns an array of child nodes at once, they become siblings.
  22. }
  23. Else
  24. {
  25. return null;
  26. }
  27. }
  28. ?>

Note: Because I am using the thinkphp framework. So in the method call some different, pure PHP file, the idea should be the same,

The first is: Write an array. An array of parent nodes.

Second: Write a method, pass the parameter is the ID of the parent node, query its child nodes, after querying in the child node, recursively continue to find child nodes of child nodes, until the final query is complete, return the array to the calling method of the parent node array. and then

 
  
  
  1. Echo (Json_encode ($arr

Transcoding it into JSON to output it for Ajax asynchronous access to get JSON data.

After that, go back to just the JS function code, directly initialize the tree directory structure, the JSON data passed to OK.

Summarize:

Its main ideas are divided into 2 steps. The first step is how the directory can be generated. When testing first, you can use static data. Similar to

 
  
  
  1. Var
  2. {Name:' parent node ', children:[{name:' child node', Children:null},{name:' sibling child node '

After you build the directory structure with fixed data

You can start thinking about the dynamic transfer of data from the directory structure to node. is what we call Ajax request PHP to get JSON data,

In PHP processing, I used a recursive algorithm to return JSON data. and completed. Directory structure.

Oh, that's right.

 
  
  
  1. $m =m (' Ztree '

This code is a thinkphp instantiated data manipulation object.

Used to query whether a node exists in the database. is the existence of child nodes, the return to the sub-node array, a few are added to the sub-node array, the query is done. Then they return at once, and they are the same child nodes.

http://www.bkjia.com/PHPjc/915111.html www.bkjia.com true http://www.bkjia.com/PHPjc/915111.html techarticle Ztree + PHP infinite pole node recursive lookup node method one, preface simple description, practice several principles, thought, actually write a lot of things, thought is the most important. 1. Target: ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.