PageRenderTime 31ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/lib/count.class.php

https://github.com/gizak/Pr
PHP | 108 lines | 80 code | 19 blank | 9 comment | 9 complexity | 63ff9ca36783ffcc4ea673f683a07c07 MD5 | raw file
  1. <?php
  2. ini_set('display_errors', '0');
  3. /*
  4. *
  5. */
  6. class count {
  7. private $dbh;//PDO object
  8. private $pdoc;//class pdoc in ./pdoc.class.php
  9. private $encoding;//default =gb2312
  10. function __construct($pdoc) {
  11. $this->dbh = $pdoc->getDBH();
  12. $this->pdoc = $pdoc;
  13. }
  14. function getCountRow($table){//get the table count
  15. $sql="select count(*) from $table";
  16. $res=$this->dbh->query($sql);
  17. return $res->fetchColumn();
  18. }
  19. function getCountArr($i) {//fetch statistical data in a array
  20. $tsql = "select * from q_t where q_num=$i";
  21. $row = $this->pdoc->getResRow($tsql);
  22. if ($row)
  23. return $ca = unserialize($row['q_a_t']);
  24. else
  25. return FALSE;
  26. }
  27. function getCountResArr($i) {//get a percentage of statistical data array
  28. $cra = $this->getCountArr($i);
  29. $sum = 0;
  30. foreach ($cra as $c)
  31. $sum+=$c;
  32. foreach ($cra as $k => $c)
  33. $cra[$k] = (float) $c / $sum;
  34. return $cra;
  35. }
  36. function getExcel($order,$en="gb2312") {//print content as a table to get execl
  37. $tsql = "select * from q_en where order_num=$order";
  38. $row = $this->pdoc->getResRow($tsql);
  39. if ($row == FALSE)
  40. return FALSE;
  41. //unserialize data from db
  42. if ($row['options'] != null)
  43. $opt = unserialize($row['options']);
  44. //get count data
  45. $optc = $this->getCountResArr($order);
  46. $optcn = $this->getCountArr($order);
  47. //print content
  48. echo "\n$order. " . iconv('utf-8', $en, $row['questions']) . "\t\n";
  49. foreach ($opt as $key => $str) {
  50. echo iconv('utf-8',$en, $str) . "\t";
  51. echo $optcn[$key] . "\t";
  52. printf("%01.2f", $optc[$key] * 100);
  53. echo "%\t\n";
  54. }
  55. }
  56. function getprintf($order) {//print content as a html table
  57. $tsql = "select * from q_en where order_num=$order";
  58. $row = $this->pdoc->getResRow($tsql);
  59. if ($row == FALSE)
  60. return FALSE;
  61. //unserialize data from db
  62. if ($row['options'] != null)
  63. $opt = unserialize($row['options']);
  64. //get count data
  65. $optc = $this->getCountResArr($order);
  66. $optcn = $this->getCountArr($order);
  67. //print content
  68. echo "<p>$order. " . $row['questions'] . "</p>";
  69. ?>
  70. <table border="1">
  71. <thead>
  72. <tr>
  73. <th>option</th>
  74. <th>count</th>
  75. <th>percentage</th>
  76. </tr>
  77. </thead>
  78. <tbody><?php foreach ($opt as $key => $str) { ?>
  79. <tr>
  80. <td><?php echo $str; ?></td>
  81. <td><?php echo $optcn[$key]; ?></td>
  82. <td><?php printf("%01.2f", $optc[$key] * 100); ?>%</td>
  83. </tr>
  84. <?php } ?>
  85. </tbody>
  86. </table>
  87. <?php }
  88. }
  89. ?>