Much more optimized bubble tree
Much more optimized bubble tree

--- a/admin/linkAmendments.php
+++ b/admin/linkAmendments.php
@@ -25,6 +25,16 @@
   else print_r($errors);
 
 }
-// also need to eliminate CN 100528/100529 - check for double parent CNs with no childCN, latest sequent CN id wins childCN = 0
+// also need to eliminate CN 100528/100529 - check for double parent CNs with no childCN, latest sequent CN id keeps childCN = 0
+$query = 'SELECT "parentCN", array_agg("CNID"), count(*) from contractnotice WHERE "parentCN" IN
+(
+      SELECT "parentCN" 
+      FROM contractnotice
+      GROUP BY "parentCN" 
+      HAVING COUNT(*) > 1 
+      AND "parentCN" != 0
+)
+AND "childCN" = 0
+GROUP BY "parentCN" having count(*) > 1';
 ?>
 

--- a/displayBubbletree.php
+++ b/displayBubbletree.php
@@ -24,62 +24,74 @@
 $color = new Lux_Color();
 
 
-$unspscresult = mysql_query("select * from UNSPSCcategories;");
-while ($row = mysql_fetch_assoc($unspscresult)) {
-	$unspsc[$row['UNSPSC']] = $row['Title'];
+$unspscresult= $conn->prepare('select * from "UNSPSCcategories";');
+$unspscresult->execute();
+foreach ($unspscresult->fetchAll() as $row) {
+  $unspsc[$row['UNSPSC']] = $row['Title'];
+
 }
 $total = 0;
-$cats = 0;
-$catsresult = mysql_query("SELECT LEFT( categoryUNSPSC, 1 ) as cat ,
-SUM( value ) as value
-FROM `contractnotice`
-WHERE childCN = 0
-GROUP BY cat ;");
+
+$catsresult = $conn->prepare('SELECT distinct substr( "categoryUNSPSC"::text, 0, 2 ) as cat 
+FROM contractnotice WHERE "categoryUNSPSC" IS NOT NULL group by "categoryUNSPSC";;');
+$catsresult->execute();
+foreach ($catsresult->fetchAll() as $row) {
+	$cats[] = $row['cat'];
+}
+$cattwosresult = $conn->prepare('SELECT distinct substr( "categoryUNSPSC"::text, 0, 3 ) as cat
+FROM contractnotice
+WHERE "categoryUNSPSC" IS NOT NULL
+GROUP BY  "categoryUNSPSC"  order by cat ;');
+$cattwosresult->execute();
+		foreach ($cattwosresult->fetchAll() as $tworow) { 
+			$cattwos[substr($tworow['cat'],0,1)][] = $tworow['cat'];
+		}
+				
+	
+
+$catthreesresult = $conn->prepare('SELECT substr( "categoryUNSPSC"::text, 0, 4 ) as cat , SUM( "value" ) as value
+FROM contractnotice
+WHERE "childCN" = 0 and "categoryUNSPSC" IS NOT NULL
+GROUP BY cat order by cat ;');
+$catthreesresult->execute();
+		foreach ($catthreesresult->fetchAll() as $threerow) { 
+			$catthrees[substr($threerow['cat'],0,2)][] = $threerow;
+		}
 $nodes = Array();
-while ($row = mysql_fetch_assoc($catsresult)) {
-$cats++;
-$catColor = $color->hsl2hex(Array($cats/10, .7, .5));
-	$catName = $unspsc[$row['cat'] . "0000000"] . $row['cat'];
-	if ($row['cat'] == "") $catName = "null";
+foreach ($cats as $catNum) {
+$catColor = $color->hsl2hex(Array($catNum/10, .7, .5));
+	$catName = $unspsc[$catNum . "0000000"] . $catNum;
 	$subnodes = Array();
-	$cattwosresult = mysql_query("SELECT LEFT( categoryUNSPSC, 2 ) as cat ,
-SUM( value ) as value
-FROM `contractnotice`
-WHERE childCN = 0 and LEFT( categoryUNSPSC, 1 ) = '{$row['cat']}'
-GROUP BY cat ;");
-	while ($tworow = mysql_fetch_assoc($cattwosresult)) {
-		$subcatName = $unspsc[$tworow['cat'] . "000000"] . $tworow['cat'];
-		if ($tworow['cat'] == "") $subcatName = "null";
+	$catValue = 0;
+	foreach ($cattwos[$catNum] as $twoCatNum) {
+		$subcatName = $unspsc[$twoCatNum. "000000"] .$twoCatNum;
 		$subsubnodes = Array();
-		$catthreesresult = mysql_query("SELECT LEFT( categoryUNSPSC, 3 ) as cat ,
-SUM( value ) as value
-FROM `contractnotice`
-WHERE childCN = 0 and LEFT( categoryUNSPSC, 2 ) = '{$tworow['cat']}'
-GROUP BY cat ;");
-		$subCatColor = $color->hsl2hex(Array($cats/10, rand(1,10)/10, .5));
-		while ($threerow = mysql_fetch_assoc($catthreesresult)) {
+	$subCatValue = 0;
+		$subCatColor = $color->hsl2hex(Array($catNum/10, rand(1,10)/10, .5));
+		foreach ($catthrees[$twoCatNum] as $threerow) {
 			$subsubcatName = $unspsc[$threerow['cat'] . "00000"] . $threerow['cat'];
-			if ($threerow['cat'] == "") $subsubcatName = "null";
 			$subsubnodes[] = Array(
 				"label" => $subsubcatName,
 				"amount" => $threerow['value'],
 				"color" => "#".$subCatColor
 			);
+			$subCatValue += $threerow['value'];
 		}
 		$subnodes[] = Array(
 			"label" => $subcatName,
-			"amount" => $tworow['value'],
+			"amount" => $subCatValue,
 			"color" => "#".$subCatColor,
 			"children" => $subsubnodes
 		);
+		$catValue += $subCatValue;
 	}
 	$nodes[] = Array(
 		"label" => $catName,
-		"amount" => $row['value'],
+		"amount" => $catValue,
 		"color" => "#".$catColor,
 		"children" => $subnodes
 	);
-	$total+= $row['value'];
+	$total += $catValue;
 }
 $data = Array(
 	"label" => "Australian Federal Government Contract Spending",