-
Notifications
You must be signed in to change notification settings - Fork 0
/
categorizedCourses.php
79 lines (64 loc) · 2.06 KB
/
categorizedCourses.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
*@author Chris Rehfeld
*/
header('content-type: application/json;charset=utf-8');
//header('content-type: text/plain;charset=utf-8');
require_once 'connection.php';
$sql = "
select site, category, title, course_link from course_data
";
$result = $dbc->query($sql);
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
$groups = array();
foreach ($rows as $row) {
extract($row);
$groups[$site][$category][] = $row;
}
//$firstCharOfCategory = substr($category, 0, 1);
$groupedAndSplitOnLetters = array();
foreach ($groups as $site => $categories) {
foreach ($categories as $category => $rows) {
if (count($rows) > 10) {
if (strlen(trim($category))) {
foreach ($rows as $row) {
$letter = substr($row['title'], 0, 1);
$groupedAndSplitOnLetters[$site][$category][$letter][] = $row;
}
} else {
unset($groupedAndSplitOnLetters[$site][$category]);
foreach ($rows as $row) {
$letter = substr($row['title'], 0, 1);
$groupedAndSplitOnLetters[$site][$letter][] = $row;
}
}
} else {
$groupedAndSplitOnLetters[$site][$category] = $rows;
}
}
}
function toD3Structure($arr) {
//check if it has numeric keys
//non-numeric means titles and names etc...
//assume first key represents all the keys types
if (is_numeric(key($arr))) {
$ret = array();
foreach ($arr as $subArr) {
//$ret[] = array('name' => $subArr['title']);
$ret[] = $subArr + array('name' => $subArr['title'], 'size' => 2);
}
return $ret;
}
$ret = array();
foreach ($arr as $key => $subArr) {
$ret[] = array('name' => $key, 'children' => toD3Structure($subArr));
}
return $ret;
}
$tree = toD3Structure($groupedAndSplitOnLetters);
echo json_encode(array('name' => 'MOOCs', 'children' => $tree));
//echo "\n\n\n";
//print_r($groupedAndSplitOnLetters);