PageRenderTime 66ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/report/quickchart_birthdayyear.php

https://bitbucket.org/3tierlogic/3tl.tabbuilder
PHP | 381 lines | 298 code | 52 blank | 31 comment | 18 complexity | 9beb636056203f8a06b5b5bb1180069e MD5 | raw file
  1. <?php
  2. require_once("db.php");
  3. $con = mysql_connect($dbhost, $dbuser, $dbpasswd); // connect to database
  4. if (!$con) { // error checking and handling
  5. die('Could not connect: ' . mysql_error());
  6. }
  7. mysql_select_db($dbname);
  8. // add where condition to support date range.
  9. function addCon($original_sql, $s, $e){
  10. // detect where;
  11. $pos = strpos($original_sql, 'where', 1);
  12. if($pos == false){ // try capital again.
  13. $pos = strpos($original_sql, 'WHERE', 1);
  14. }
  15. $sub1 = substr($original_sql,0,$pos+1+4); // "select...where"
  16. //print "sub1: ".$sub1."<br/>";
  17. $sub2 = " cpresult_create_time between '".$s."' and '".$e."' and ";
  18. //print "sub2: ".$sub2."<br/>";
  19. $sub3 = substr($original_sql, $pos+1+4+1); // " ..."
  20. //print "sub3: ".$sub3."<br/>";
  21. $original_sql = "";
  22. $original_sql = $original_sql.$sub1;
  23. $original_sql = $original_sql.$sub2;
  24. $original_sql = $original_sql.$sub3;
  25. return $original_sql;
  26. }
  27. function nichi($raw){
  28. // 11/16 to 2011-11-16
  29. $done = str_replace('/',"-",$raw);
  30. //echo "done = ".$done."<br/>";
  31. $done = "2011-".$done;
  32. //echo "done = ".$done."<br/>";
  33. return $done;
  34. }
  35. // start of code.
  36. $campaignID = $_REQUEST['cid'];
  37. $sDate = $_REQUEST['sdate'];
  38. $eDate = $_REQUEST['edate'];
  39. $chartType = $_REQUEST['ctype'];
  40. $countryCode = $_REQUEST['c'];
  41. //echo $chartType."<br>";
  42. switch ($chartType) {
  43. case "birthdayyear":
  44. $currentyear = date("Y");
  45. $agerangestr_data = "";
  46. //**** Check for under 18 - START ******
  47. $yearlimit = $currentyear - 18;
  48. $sql_bday = "SELECT cpresult_birth_year FROM sp_campaign_result where campaign_sid='".$campaignID."' AND cpresult_birth_year > ".$yearlimit;
  49. $sql_bday = addCon($sql_bday, $sDate, $eDate);
  50. $res_bday = mysql_query($sql_bday, $con);
  51. if (mysql_num_rows($res_bday) > 0) {
  52. $agerangestr_data = $agerangestr_data."['Under 18', ".mysql_num_rows($res_bday)."],";
  53. }
  54. $b0_17 = mysql_num_rows($res_bday);
  55. $birthdayArray[0] = "Under 18";
  56. $birthdayTotalArray[0] = $b0_17;
  57. //}
  58. //**** Check for 18-24 range - START ******
  59. $yearlimit_high = $currentyear - 18;
  60. $yearlimit_low = $currentyear - 24;
  61. $sql_bday = "SELECT cpresult_birth_year FROM sp_campaign_result where campaign_sid='".$campaignID."' AND cpresult_birth_year >= ".$yearlimit_low." AND cpresult_birth_year <= ".$yearlimit_high;
  62. $sql_bday = addCon($sql_bday, $sDate, $eDate);
  63. $res_bday = mysql_query($sql_bday, $con);
  64. if (mysql_num_rows($res_bday) > 0) {
  65. $agerangestr_data = $agerangestr_data."['18 - 24', ".mysql_num_rows($res_bday)."],";
  66. }
  67. $b18_24 = mysql_num_rows($res_bday);
  68. $birthdayArray[1] = "18 - 24";
  69. $birthdayTotalArray[1] = $b18_24;
  70. //}
  71. //**** Check for Check 25 - 34 range - START ******
  72. $yearlimit_high = $currentyear - 25;
  73. $yearlimit_low = $currentyear - 34;
  74. $sql_bday = "SELECT cpresult_birth_year FROM sp_campaign_result where campaign_sid='".$campaignID."' AND cpresult_birth_year >= ".$yearlimit_low." AND cpresult_birth_year <= ".$yearlimit_high;
  75. $sql_bday = addCon($sql_bday, $sDate, $eDate);
  76. $res_bday = mysql_query($sql_bday, $con);
  77. if (mysql_num_rows($res_bday) > 0) {
  78. $agerangestr_data = $agerangestr_data."['25 - 34', ".mysql_num_rows($res_bday)."],";
  79. }
  80. $b25_34 = mysql_num_rows($res_bday);
  81. $birthdayArray[2] = "25 - 34";
  82. $birthdayTotalArray[2] = $b25_34;
  83. //}
  84. //**** Check for 35 - 44 range - START ******
  85. $yearlimit_high = $currentyear - 35;
  86. $yearlimit_low = $currentyear - 44;
  87. $sql_bday = "SELECT cpresult_birth_year FROM sp_campaign_result where campaign_sid='".$campaignID."' AND cpresult_birth_year >= ".$yearlimit_low." AND cpresult_birth_year <= ".$yearlimit_high;
  88. $sql_bday = addCon($sql_bday, $sDate, $eDate);
  89. $res_bday = mysql_query($sql_bday, $con);
  90. if (mysql_num_rows($res_bday) > 0) {
  91. $agerangestr_data = $agerangestr_data."['35 - 44', ".mysql_num_rows($res_bday)."],";
  92. }
  93. $b35_44 = mysql_num_rows($res_bday);
  94. $birthdayArray[3] = "35 - 44";
  95. $birthdayTotalArray[3] = $b35_44;
  96. //}
  97. //**** Check for 45 - 54 range - START ******
  98. $yearlimit_high = $currentyear - 45;
  99. $yearlimit_low = $currentyear - 54;
  100. $sql_bday = "SELECT cpresult_birth_year FROM sp_campaign_result where campaign_sid='".$campaignID."' AND cpresult_birth_year >= ".$yearlimit_low." AND cpresult_birth_year <= ".$yearlimit_high;
  101. $sql_bday = addCon($sql_bday, $sDate, $eDate);
  102. $res_bday = mysql_query($sql_bday, $con);
  103. if (mysql_num_rows($res_bday) > 0) {
  104. $agerangestr_data = $agerangestr_data."['45 - 54', ".mysql_num_rows($res_bday)."],";
  105. }
  106. $b45_54 = mysql_num_rows($res_bday);
  107. $birthdayArray[4] = "45 - 54";
  108. $birthdayTotalArray[4] = $b45_54;
  109. //}
  110. //**** Check for 55 - 64 range - START ******
  111. $yearlimit_high = $currentyear - 55;
  112. $yearlimit_low = $currentyear - 64;
  113. $sql_bday = "SELECT cpresult_birth_year FROM sp_campaign_result where campaign_sid='".$campaignID."' AND cpresult_birth_year >= ".$yearlimit_low." AND cpresult_birth_year <= ".$yearlimit_high;
  114. $sql_bday = addCon($sql_bday, $sDate, $eDate);
  115. $res_bday = mysql_query($sql_bday, $con);
  116. if (mysql_num_rows($res_bday) > 0) {
  117. $agerangestr_data = $agerangestr_data."['55 - 64', ".mysql_num_rows($res_bday)."],";
  118. }
  119. $b55_64 = mysql_num_rows($res_bday);
  120. $birthdayArray[5] = "55 - 64";
  121. $birthdayTotalArray[5] = $b55_64;
  122. //}
  123. //**** Check for 65+ range - START ******
  124. $yearlimit = $currentyear - 65;
  125. $sql_bday = "SELECT cpresult_birth_year FROM sp_campaign_result where campaign_sid='".$campaignID."' AND cpresult_birth_year <= ".$yearlimit ." AND cpresult_birth_year > 0";
  126. $res_bday = mysql_query($sql_bday, $con);
  127. $sql_bday = addCon($sql_bday, $sDate, $eDate);
  128. if (mysql_num_rows($res_bday) > 0) {
  129. $agerangestr_data = $agerangestr_data."['65+', ".mysql_num_rows($res_bday)."],";
  130. }
  131. $b65_99 = mysql_num_rows($res_bday);
  132. $birthdayArray[6] = "65+";
  133. $birthdayTotalArray[6] = $b65_99;
  134. //}
  135. //** Check year=0 (No year entered)
  136. $sql_bday = "SELECT cpresult_birth_year FROM sp_campaign_result where campaign_sid='".$campaignID."' AND cpresult_birth_year = 0";
  137. $sql_bday = addCon($sql_bday, $sDate, $eDate);
  138. $res_bday = mysql_query($sql_bday, $con);
  139. if (mysql_num_rows($res_bday) > 0) {
  140. $agerangestr_data = $agerangestr_data."{name:'No age data', y:".mysql_num_rows($res_bday).", sliced:true, selected:true}";
  141. }
  142. $b0_0 = mysql_num_rows($res_bday);
  143. $birthdayArray[7] = "No age data";
  144. $birthdayTotalArray[7] = $b0_0 ;
  145. //}
  146. //echo $agerangestr_data;
  147. $displayagerange_div = 1;
  148. break;
  149. }
  150. ?>
  151. <html>
  152. <head>
  153. <link href="style.css" rel="stylesheet" type="text/css" />
  154. <style>
  155. div.panel{
  156. height:auto;
  157. display:none;
  158. }
  159. table#user-list tr.hover {
  160. color: #fff;
  161. background-color: #4c95e6;
  162. }
  163. #containergraph {
  164. float: left
  165. width: 33.3%;
  166. /* Min-height: */
  167. min-height: 400px;
  168. height: auto !important;
  169. height: 400px;
  170. }
  171. </style>
  172. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  173. <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
  174. <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
  175. <!-- Charts JS library: START -->
  176. <script type="text/javascript" src="js/highcharts.js"></script>
  177. <script type="text/javascript" src="js/themes/gray.js"></script>
  178. <script type="text/javascript" src="js/modules/exporting.js"></script>
  179. <!-- Charts JS library: END -->
  180. <style type="text/css">
  181. @import "DTmedia/css/demo_page.css";
  182. @import "DTmedia/css/demo_table.css";
  183. </style>
  184. <script class="jsbin" src="http://datatables.net/download/build/jquery.dataTables.js"></script>
  185. <script>
  186. var chart;
  187. var field="<?=$chartType?>";
  188. $(document).ready(function() {
  189. $('#example').dataTable({
  190. "bJQueryUI": true,
  191. <!--"sPaginationType": "full_numbers",-->
  192. "bPaginate": false,
  193. "bFilter": false,
  194. "bLengthChange": false,
  195. "bScrollInfinite": true,
  196. "bScrollCollapse": true,
  197. "sScrollY": "<?php echo $tableHeight ?>px"
  198. });
  199. switch(field){
  200. case "birthdayyear":{
  201. chart = new Highcharts.Chart({
  202. chart: {
  203. renderTo: 'container',
  204. plotBackgroundColor: null,
  205. plotBorderWidth: null,
  206. plotShadow: false
  207. },
  208. title: {
  209. text: 'Age range breakdown'
  210. },
  211. tooltip: {
  212. formatter: function() {
  213. return '<b>'+ this.point.name +'</b>: '+ this.y +' entries';
  214. //return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
  215. }
  216. },
  217. plotOptions: {
  218. pie: {
  219. allowPointSelect: true,
  220. cursor: 'pointer',
  221. dataLabels: {
  222. enabled: true,
  223. color: Highcharts.theme.textColor || '#000000',
  224. connectorColor: Highcharts.theme.textColor || '#000000',
  225. formatter: function() {
  226. //return '<b>'+ this.point.name +'</b>: '+ this.y +' entries';
  227. return '<b>'+ this.point.name +'</b>: '+ Highcharts.numberFormat(this.percentage, 2) +' %';
  228. }
  229. }
  230. }
  231. },
  232. series: [{
  233. type: 'pie',
  234. name: 'Age Range breakdown',
  235. data: [
  236. <?php echo $agerangestr_data;?>
  237. ]
  238. }]
  239. });
  240. break;
  241. }
  242. default: {
  243. chart = new Highcharts.Chart({
  244. chart: {
  245. renderTo: 'container',
  246. plotBackgroundColor: null,
  247. plotBorderWidth: null,
  248. plotShadow: false
  249. },
  250. title: {
  251. text: 'Browser market shares at a specific website, 2010'
  252. },
  253. tooltip: {
  254. formatter: function() {
  255. return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
  256. }
  257. },
  258. plotOptions: {
  259. pie: {
  260. allowPointSelect: true,
  261. cursor: 'pointer',
  262. dataLabels: {
  263. enabled: true,
  264. color: Highcharts.theme.textColor || '#000000',
  265. connectorColor: Highcharts.theme.textColor || '#000000',
  266. formatter: function() {
  267. return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
  268. }
  269. }
  270. }
  271. },
  272. series: [{
  273. type: 'pie',
  274. name: 'Browser share',
  275. data: [
  276. ['Firefox', 45.0],
  277. ['IE', 26.8],
  278. {
  279. name: 'Chrome',
  280. y: 12.8,
  281. sliced: true,
  282. selected: true
  283. },
  284. ['Safari', 8.5],
  285. ['Opera', 6.2],
  286. ['Others', 0.7]
  287. ]
  288. }]
  289. });
  290. }
  291. } //*** Switch
  292. }); //**** function
  293. </script>
  294. </head>
  295. <body>
  296. <div id="container" style="width: 900px; height: 400px; margin: auto;"></div>
  297. <div id="tableContainer" style="width: 900px; height: 400px; margin: auto;" >
  298. <br><br><br><br>
  299. <?php
  300. echo "<h2>Campaign summary from ".$sDate." to ".$eDate."</h2>";
  301. echo "<table cellpadding='0' cellspacing='0' border='2' class='display' id='example'>"; //id='user-list'>";
  302. echo "<thead><tr><th>Item</th><th>Age Range</th><th>Total Entries</th></tr></thead>";
  303. echo "<tbody>";
  304. $counter = 0;
  305. $t = 0;
  306. foreach ($birthdayArray as $agerangelabel) {
  307. if($counter % 2 != 0){
  308. echo "<tr class='odd gradeX'>";
  309. }else{
  310. echo "<tr class='odd gradeA'>";
  311. }
  312. echo "<td class='center'>".($counter+1)."</td>";
  313. echo "<td class='center'>".$agerangelabel."</td>";
  314. echo "<td class='center'>".$birthdayTotalArray[$counter]."</td>";
  315. echo "</tr>";
  316. $t = $t + $birthdayTotalArray[$counter];
  317. $counter++;
  318. }
  319. echo "<tr class='odd gradeA'>";
  320. echo "<td class='center'>".($counter+1)."</td>";
  321. echo "<td class='center'><b>Total entries</b></td>";
  322. echo "<td class='center'><b>".$t."</b></td>";
  323. echo "</tr>";
  324. echo "</tbody>";
  325. echo "</table>";
  326. ?>
  327. </div>
  328. </body>
  329. </html>