Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. <?php
  2.  
  3. class Tree{
  4.  
  5. private $db;
  6. public $data;
  7.  
  8. public function __construct()
  9. {
  10. $this->db = new PDO('mysql:host=localhost;dbname=tree', 'root', '');
  11. $this->db->query("SET NAMES utf8");
  12. }
  13.  
  14. public function ShowTree($id)
  15. {
  16.  
  17. $sql = "SELECT * FROM categories WHERE p_id=".$id."";
  18. $res = $this->db->query($sql);
  19. $this->data .= "<ul>";
  20. while ($result = $res->fetch(PDO::FETCH_ASSOC)) {
  21. $this->data .= "<li>";
  22. $this->data .= $result["name"];
  23. $this->data .= "</li>";
  24. $this->showTree($result["id"], $this->db);
  25. }
  26. $this->data .= "</ul>";
  27.  
  28. return $this->data;
  29. }
  30. }
  31.  
  32.  
  33. $tree = new Tree();
  34. echo $tree->ShowTree(0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement