SHARE
TWEET

Untitled

a guest Jun 16th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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": "unit",
  51.                         "children": [{
  52.                             "name": "Lecture - CVS",
  53.                             "type": "learning_event"
  54.                         }, {
  55.                             "name": "Lecture - Type 1 Diabetes",
  56.                             "type": "learning_event"
  57.                         }]
  58.                     }]
  59.                 }, {...
  60.      
  61. $query = "SELECT CONCAT('program:', program_pk) AS global_id,
  62.        program_name AS name,
  63.        NULL AS parent_global_id
  64. FROM program
  65. UNION ALL
  66. SELECT CONCAT('theme:', theme_pk) AS global_id,
  67.        theme_name AS name,
  68.        CONCAT('program:', program_fk) AS parent_global_id
  69. FROM theme
  70. UNION ALL
  71. SELECT
  72.        CONCAT('theme:', theme_fk, ',strand:', strand_name) AS global_id,
  73.        strand_name AS name,
  74.        CONCAT('theme:', theme_fk) AS parent_global_id
  75. FROM strand
  76. UNION ALL
  77. SELECT
  78.        CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name) AS global_id,
  79.        strandyear_name AS name,
  80.        CONCAT('theme:', theme_fk, ',strand:', strand_name) AS parent_global_id
  81. FROM strandyear sy
  82. INNER JOIN strand s ON s.strand_pk = sy.strand_fk
  83.  
  84. UNION ALL
  85. SELECT
  86.        CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name, ',unit:', unit_name) AS global_id,
  87.        unit_name AS name,
  88.        CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name) AS parent_global_id
  89. FROM unit u
  90. INNER JOIN strandyear sy ON u.strandyear_fk = sy.strandyear_pk
  91. INNER JOIN strand s ON s.strand_pk = sy.strand_fk
  92.  
  93. UNION ALL
  94. SELECT
  95.        CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name, ',unit:', unit_name, ',learning_event:', learning_event_name) AS global_id,
  96.        learning_event_name AS name,
  97.        CONCAT('theme:', theme_fk, ',strand:', strand_name, ',strandyear:', strandyear_name, ',unit:', unit_name) AS parent_global_id
  98. FROM learning_event le
  99. INNER JOIN unit u ON u.unit_pk = le.unit_fk
  100. INNER JOIN strandyear sy ON u.strandyear_fk = sy.strandyear_pk
  101. INNER JOIN strand s ON s.strand_pk = sy.strand_fk";
  102. $result = $connection->query($query);
  103. $data = array();
  104. while ($row = $result->fetch_object()) {
  105.     $data[$row->global_id] = $row;
  106. }
  107.  
  108. $roots = array();
  109. foreach ($data as $row) {  
  110.     if ($row->parent_global_id === null) {
  111.         $roots[]= $row;
  112.     } else {
  113.         $data[$row->parent_global_id]->children[] = $row;
  114.     }
  115.     unset($row->parent_global_id);
  116.     unset($row->global_id);
  117. }
  118.  
  119. $json = json_encode($roots);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top