Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. {
  2. "name": "MD",
  3. "children": [{
  4. "name": "Professional",
  5. "children": [{
  6. "name": "Professional Behavours",
  7. "children": [{
  8. "name": "Year 1",
  9. "children": [{
  10. "name": "Integrated Medical Sciences 1"
  11. }, {
  12. "name": "Integrated Medical Sciences 2"
  13. }]
  14. }, {
  15. "name": "Year 2",
  16. "children": [{
  17. "name": "Integrated Medical Practice 1",
  18. "children": [{
  19. "name": "Lecture - CVS"
  20. }, {
  21. "name": "Lecture - Type 1 Diabetes"
  22. }]
  23. }]
  24. }, {...
  25.  
  26. {
  27. "name": "MD",
  28. "type": "program",
  29. "children": [{
  30. "name": "Professional",
  31. "type": "theme",
  32. "children": [{
  33. "name": "Professional Behavours",
  34. "type": "strand",
  35. "children": [{
  36. "name": "Year 1",
  37. "type": "strandyear",
  38. "children": [{
  39. "name": "Integrated Medical Sciences 1",
  40. "type": "unit"
  41. }, {
  42. "name": "Integrated Medical Sciences 2",
  43. "type": "unit"
  44. }]
  45. }, {
  46. "name": "Year 2",
  47. "type": "strandyear",
  48. "children": [{
  49. "name": "Integrated Medical Practice 1",
  50. "type": "learning_event",
  51. "children": [{
  52. "name": "Lecture - CVS"
  53. }, {
  54. "name": "Lecture - Type 1 Diabetes",
  55. "type": "learning_event"
  56. }]
  57. }]
  58. }, {...
  59.  
  60. $query = "SELECT CONCAT('program:', program_pk) AS global_id,
  61. program_name AS name,
  62. NULL AS parent_global_id
  63. FROM program
  64. UNION ALL
  65. SELECT CONCAT('theme:', theme_pk) AS global_id,
  66. theme_name AS name,
  67. CONCAT('program:', program_fk) AS parent_global_id
  68. FROM theme
  69. UNION ALL
  70. SELECT
  71. CONCAT('theme:', theme_fk, ',strand:', strand_name) AS global_id,
  72. strand_name AS name,
  73. CONCAT('theme:', theme_fk) AS parent_global_id
  74. FROM strand
  75. UNION ALL
  76. SELECT
  77. CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name) AS global_id,
  78. strandyear_name AS name,
  79. CONCAT('theme:', theme_fk, ',strand:', strand_name) AS parent_global_id
  80. FROM strandyear sy
  81. INNER JOIN strand s ON s.strand_pk = sy.strand_fk
  82.  
  83. UNION ALL
  84. SELECT
  85. CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name, ',unit:', unit_name) AS global_id,
  86. unit_name AS name,
  87. CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name) AS parent_global_id
  88. FROM unit u
  89. INNER JOIN strandyear sy ON u.strandyear_fk = sy.strandyear_pk
  90. INNER JOIN strand s ON s.strand_pk = sy.strand_fk
  91.  
  92. UNION ALL
  93. SELECT
  94. CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name, ',unit:', unit_name, ',learning_event:', learning_event_name) AS global_id,
  95. learning_event_name AS name,
  96. CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name, ',unit:', unit_name) AS parent_global_id
  97. FROM learning_event le
  98. INNER JOIN unit u ON u.unit_pk = le.unit_fk
  99. INNER JOIN strandyear sy ON u.strandyear_fk = sy.strandyear_pk
  100. INNER JOIN strand s ON s.strand_pk = sy.strand_fk";
  101. $result = $connection->query($query);
  102. $data = array();
  103. while ($row = $result->fetch_object()) {
  104. $data[$row->global_id] = $row;
  105. }
  106.  
  107. $roots = array();
  108. foreach ($data as $row) {
  109. if ($row->parent_global_id === null) {
  110. $roots[]= $row;
  111. } else {
  112. $data[$row->parent_global_id]->children[] = $row;
  113. }
  114. unset($row->parent_global_id);
  115. unset($row->global_id);
  116. }
  117.  
  118. $json = json_encode($roots);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement