/xml/freq_xml.php

https://bitbucket.org/royrutto/climatepal · PHP · 117 lines · 97 code · 8 blank · 12 comment · 3 complexity · 6411bbc2befa3b0e80bd0bafc1090566 MD5 · raw file

  1. <?php
  2. include("DBConn.php");
  3. $link = connectToDB();
  4. $monthnames=array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  5. $sql_freq=mysql_query("SELECT options FROM `quest_fields` WHERE field_id IN (SELECT field_id FROM quest_fields WHERE field_name='How often are you using the Hifadhi stove?');") or die(mysql_error());
  6. $sql_resultset=mysql_fetch_assoc($sql_freq);
  7. $sql_valueset=$sql_resultset['options'];
  8. $string_exploded = explode(",",$sql_valueset);
  9. if(!isset($_GET['parameters'])){
  10. $result = date('Y');
  11. //echo $year;
  12. $sql_months = "select distinct MONTH(date_added) as month from quest_field_results WHERE YEAR(date_added) = $result order by month asc;";
  13. $sql_result_months = mysql_query($sql_months) or die(mysql_error());
  14. $months = array();
  15. $counter = 0;
  16. while($month = mysql_fetch_assoc($sql_result_months)){
  17. $months[$counter] = $month['month'];
  18. //echo $monthnames[$months[$counter]-1];
  19. $counter++;
  20. }
  21. ?>
  22. <chart palette='2' caption='Frequency of use of Hifadhi Stoves' subCaption='For the year <?php echo $result; ?>' lineThickness='1' showValues='0' decimals='0' formatNumberScale='0' xAxisName='Month' yAxisName='Number of Stoves' numberSuffix=' ' useRoundEdges='0' numvdivlines='5'>
  23. <categories >
  24. <?php
  25. foreach($months as $dist_month){?>
  26. <category label='<?php echo $monthnames[$dist_month-1]." ".$result; ?>'/>";
  27. <?php
  28. }
  29. ?>
  30. </categories> <?php
  31. foreach($months as $dist_month){
  32. for($i=0;$i<count($string_exploded);$i++){?>
  33. <dataset seriesName='<?php echo $string_exploded[$i]; ?>'>;
  34. <?php
  35. $sql_get_data = "SELECT COUNT(*) AS total_distributed FROM quest_field_results WHERE field_id IN (SELECT field_id FROM quest_fields WHERE field_name='How often are you using the Hifadhi stove?') AND value='".$string_exploded[$i]."' AND MONTH(date_added) = $dist_month AND YEAR(date_added) = $result;";
  36. //echo $sql_get_data;
  37. $sql_result_get_data = mysql_query($sql_get_data) or die(mysql_error());
  38. $get_data_resultset = mysql_fetch_assoc($sql_result_get_data);
  39. $distributed = $get_data_resultset['total_distributed'];
  40. //echo $distributed; ?>
  41. <set value='<?php echo $distributed; ?>'/>;
  42. </dataset>;
  43. <?php
  44. }
  45. //free the resultset
  46. //mysql_free_result($get_data_resultset);
  47. //echo $monthnames[1]." ".$distributed.'<br />';
  48. }
  49. $counter++;
  50. }
  51. else{
  52. $parameters = explode('_',$_GET['parameters']);
  53. $from = $parameters[0];
  54. $to = $parameters[1];
  55. $sql_months = "select distinct MONTH(date_added) as month, YEAR(date_added) as year from quest_field_results WHERE date_added BETWEEN '".$from."' AND '".$to."';";
  56. $sql_result_months = mysql_query($sql_months) or die(mysql_error());
  57. $months = array();
  58. $years = array();
  59. $counter = 0;
  60. while($row = mysql_fetch_assoc($sql_result_months)){
  61. $months[$counter] = $row['month'];
  62. $years[$counter] = $row['year'];
  63. $counter++;
  64. }
  65. ?>
  66. <chart palette='2' caption='Frequency of use of Hifadhi Stoves' subCaption='From: <?php echo $from;?> To: <?php echo $to;?>' lineThickness='1' showValues='0' decimals='0' formatNumberScale='0' xAxisName='Month' yAxisName='Number of Stoves' numberSuffix=' ' useRoundEdges='0' numvdivlines='5'>
  67. <categories >
  68. <?php
  69. $x=0;
  70. foreach($months as $dist_month){?>
  71. <category label='<?php echo $monthnames[$dist_month-1]." ".$years[$x]; ?>'/>";
  72. <?php
  73. $x++;
  74. }
  75. ?>
  76. </categories> <?php
  77. $y=0;
  78. foreach($months as $dist_month){
  79. for($i=0;$i<count($string_exploded);$i++){?>
  80. <dataset seriesName='<?php echo $string_exploded[$i]; ?>'>;
  81. <?php
  82. $sql_get_data = "SELECT COUNT(*) AS total_distributed FROM quest_field_results WHERE field_id IN (SELECT field_id FROM quest_fields WHERE field_name='How often are you using the Hifadhi stove?') AND value='".$string_exploded[$i]."' AND MONTH(date_added) = $dist_month AND YEAR(date_added) = '".$years[$y]."';";
  83. //echo $sql_get_data;
  84. $sql_result_get_data = mysql_query($sql_get_data) or die(mysql_error());
  85. $get_data_resultset = mysql_fetch_assoc($sql_result_get_data);
  86. $distributed = $get_data_resultset['total_distributed'];
  87. //echo $distributed; ?>
  88. <set value='<?php echo $distributed; ?>'/>;
  89. </dataset>;
  90. <?php
  91. }
  92. $y++;
  93. //free the resultset
  94. //mysql_free_result($get_data_resultset);
  95. //echo $monthnames[1]." ".$distributed.'<br />';
  96. }
  97. $counter++;
  98. }
  99. ?>
  100. <styles>
  101. <definition>
  102. <style name='CaptionFont' type='font' size='12' />
  103. </definition>
  104. <application>
  105. <apply toObject='CAPTION' styles='CaptionFont' />
  106. <apply toObject='SUBCAPTION' styles='CaptionFont' />
  107. </application>
  108. </styles>
  109. </chart>