Advertisement
cdsatrian

Organization Chart

Dec 28th, 2012
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.23 KB | None | 0 0
  1. <?php
  2. /*********************************
  3. FILENAME  : orgchart.php
  4. CREATE BY  : cahya dsn
  5. PURPOSE   : display organization chart report
  6. CREATE DATE : 2012-12-28
  7. **********************************
  8. #table creation
  9.  
  10. USE `test`;
  11.  
  12. DROP TABLE IF EXISTS `tbl_personel`;
  13. CREATE TABLE IF NOT EXISTS `tbl_personel` (
  14.  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key untuk table tbl_personel',
  15.  `nama` varchar(30) NOT NULL COMMENT 'nama personel',
  16.  `id_parent` bigint(20) unsigned NOT NULL DEFAULT 0,
  17.  PRIMARY KEY(`id`)
  18. ) ENGINE=MyISAM COMMENT='tabel untuk menyimpan data personel';
  19.  
  20. INSERT INTO `tbl_personel`(`id`,`nama`,`id_parent`) VALUES
  21. (NULL,'Food',NULL),
  22. (NULL,'Beer',1),
  23. (NULL,'Vegetables',1),
  24. (NULL,'Fruit',1),
  25. (NULL,'Bread',1),
  26. (NULL,'Chocolate',1),
  27. (NULL,'Pumpkin',3),
  28. (NULL,'Aubergine',3),
  29. (NULL,'Apple',4),
  30. (NULL,'Berries',4),
  31. (NULL,'Granny Smith',9),
  32. (NULL,'Blueberry',10),
  33. (NULL,'Redberry',10),
  34. (NULL,'Cucumber',10);
  35. */
  36. //database configuration
  37. $dbhost='localhost';
  38. $dbuser='root';
  39. $dbpass='';
  40. $dbname='test';
  41. //database connection
  42. $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  43. //query to get organization datas from database
  44. $sql="SELECT * FROM tbl_personel ORDER BY id_parent,id ASC";
  45. $result=$db->query($sql);
  46. //variables initialization
  47. $data=array();
  48. $i=0;
  49. //generate organization datas from database
  50. // 1. Fetch data from database
  51. while($records=$result->fetch_assoc()){
  52.   $data[$i]=$records;
  53.   $i++;
  54. }
  55. //--------------------------------
  56. // 2. Generate Data array from database data
  57. function GenerateDataArray($arr, $parent = 0)
  58. {
  59.   $pages = Array();
  60.   foreach($arr as $page)
  61.   {
  62.     if($page['id_parent'] == $parent)
  63.     {
  64.       $page['sub'] = isset($page['sub']) ? $page['sub'] : GenerateDataArray($arr, $page['id']);
  65.       $pages[] = $page;
  66.     }
  67.   }
  68.   return $pages;
  69. }
  70. $dataarray = GenerateDataArray($data);
  71. //-----------------------------------
  72. //3. loop the multidimensional array recursively to generate the HTML code
  73. function GenerateDataHTML($data,$top='true')
  74. {
  75.   $html = '';
  76.   foreach($data as $page)
  77.   {
  78.     if(is_array($page['sub']) && !empty($page['sub'])) {
  79.       $html .= "<li>\n";
  80.       $html .= "<a href=\"".$page['id']."\">". $page['nama']."</a>";
  81.       $html .= "<ul>".GenerateDataHTML($page['sub'],false)."</ul>\n";
  82.       $html .= "</li>\n";
  83.     }else{
  84.       $html.="<li><a href=\"".$page['id']."\">".$page['nama']."</a></li>";
  85.     }
  86.   }
  87.   return $html;
  88. }
  89. ?>
  90. <!DOCTYPE html>
  91. <html>
  92. <head>
  93. <title>Organization Chart</title>
  94. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
  95. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
  96. <script type="text/javascript" src="http://dl.dropbox.com/u/4151695/html/jOrgChart/example/jquery.jOrgChart.js"></script>
  97. <link rel="stylesheet" href="http://dl.dropbox.com/u/4151695/html/jOrgChart/example/css/jquery.jOrgChart.css"/>
  98. <script>
  99.   jQuery(document).ready(function() {
  100.      $("#org").jOrgChart({
  101.             dragAndDrop  : true
  102.         });
  103.   });
  104. </script>
  105. </head>
  106. <body>
  107. <ul id="org" style="display:none">
  108. <?php echo GenerateDataHTML($dataarray);?>  
  109. </ul>
  110. </body>
  111. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement