Guest User

Untitled

a guest
Dec 2nd, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.47 KB | None | 0 0
  1. <?php
  2. /******** 120922 by cahyadsn@yahoo.com ********
  3. -- table category
  4. DROP TABLE IF EXISTS `category`;
  5. CREATE TABLE IF NOT EXISTS `category` (
  6.   `id` int(11) UNSIGNED NOT NULL,
  7.   `parent_id`  int(11) UNSIGNED NOT NULL DEFAULT '0',
  8.   `name` varchar(100) NOT NULL,
  9.   PRIMARY KEY (`id`)
  10. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  11. --
  12. -- example
  13. --
  14. INSERT INTO `category` (`id`, `parent_id`, `name`) VALUES
  15. (1,0,"node no 1"),
  16. (2,0,"node no  2"),
  17. (3,1,"node no  1.1"),
  18. (4,2,"node no  2.1"),
  19. (5,3,"node no  3.1.1"),
  20. (6,3,"node no  3.1.2");
  21. *************************************************/
  22. class category{
  23.   var $mysqli;
  24.  
  25.   function __construct(){
  26.     //--- database configuration
  27.     $dbhost="localhost";
  28.     $dbuser="root";
  29.     $dbpass="";
  30.     $dbname="test";
  31.     //---- initiate database connection
  32.     $this->mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
  33.     if (mysqli_connect_error()) {
  34.       die('Connect Error (' . mysqli_connect_errno() . ') '
  35.             . mysqli_connect_error());
  36.     }
  37.   }
  38.  
  39.   function hasChild($parent_id)
  40.   {
  41.     $sql = "SELECT COUNT(*) as count FROM category WHERE parent_id = ' " . $parent_id . " ' ";
  42.     $rs=$this->mysqli->query($sql)->fetch_object();
  43.     return $rs->count;
  44.   }
  45.  
  46.   function CategoryTree($list,$parent,$append)
  47.   {
  48.     $list = '<li>'.$parent->name.'</li>';
  49.     if ($this->hasChild($parent->id)) // check if the id has a child
  50.     {
  51.       $append++; // this is our basis on what level is the category e.g. (child1,child2,child3)
  52.       $list .= "<ul class='child child".$append." '>";
  53.       $sql = "SELECT * FROM category WHERE parent_id = ' " . $parent->id . " ' ";
  54.       $obj=$this->mysqli->query($sql);
  55.       $child=$obj->fetch_object();
  56.       do{
  57.         $list .= $this->CategoryTree($list,$child,$append);
  58.       }while($child = $obj->fetch_object());
  59.       $list .= "</ul>";
  60.     }
  61.     return $list;
  62.   }
  63.   function CategoryList()
  64.   {
  65.     $list = "";
  66.     $sql = "SELECT * FROM category WHERE (parent_id = 0 OR parent_id IS NULL)";
  67.     $obj=$this->mysqli->query($sql);
  68.     $parent=$obj->fetch_object();
  69.     $mainlist = "<ul class='parent'>";
  70.     do{
  71.       $mainlist .= $this->CategoryTree($list,$parent,$append = 0);
  72.     }while($parent = $obj->fetch_object());
  73.     $list .= "</ul>";
  74.     return $mainlist;
  75.   }
  76.   function __destruct()
  77.   {
  78.     $this->mysqli->close();
  79.   }
  80. }  
  81.  
  82. //===================
  83. $category=new category();
  84. echo $category->CategoryList();
  85.  
  86. ?>
Add Comment
Please, Sign In to add comment