Advertisement
astropos

UserSpice JSON Graph Data

Jan 25th, 2016
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. // Flot graph with JSON data via AJAX for UserSpice 3.0.4b
  2. //
  3. // *** NOTE: This requires updating your root files with the footer modification in v3.1x
  4. //
  5. // If you have problems, update to the latest flot
  6. //
  7. // Feedback invited
  8.  
  9. Provides sample data (user/permissions count) for:
  10. Flot pie-chart
  11.  
  12. // create a new file in the root of your website named json_chart.php
  13. // set UserSpice permissions on this file as required (after installation)
  14. // paste the following code:
  15.  
  16. <?php
  17. /*
  18. UserSpice 3.0.4b
  19. by Dan Hoover at http://UserSpice.com
  20.  
  21. a modern version of
  22. UserCake Version: 2.0.2
  23.  
  24. UserCake created by: Adam Davis
  25. UserCake V2.0 designed by: Jonathan Cassels
  26.  
  27. */
  28. require_once("models/config.php");
  29. if (!securePage($_SERVER['PHP_SELF'])){die();}
  30.  
  31. $newArray[] = array();
  32. $makeArray = fetchUserjson();
  33. $i = 0;
  34.  
  35. foreach ($makeArray as $v1)
  36. {
  37. $permid = $v1['permission_id'];
  38. $value = $v1['sum1'];
  39. $name = $v1['name'];
  40.  
  41. $newArray[$i] = array(
  42. "label" => $name,
  43. "data" => $value
  44. );
  45. $i++;
  46. }
  47.  
  48. echo json_encode($newArray);
  49. ?>
  50.  
  51.  
  52.  
  53.  
  54. // In models/funcs.php (or your user defined funcs)
  55. // add the following code:
  56.  
  57. function fetchUserjson()
  58. {
  59. global $mysqli,$db_table_prefix;
  60. $stmt = $mysqli->prepare("SELECT
  61. COUNT(permission_id) AS sum1,
  62. permission_id,name
  63. FROM ".$db_table_prefix."user_permission_matches LEFT JOIN ".$db_table_prefix."permissions ON permission_id = ".$db_table_prefix."permissions.id GROUP BY permission_id");
  64. $stmt->execute();
  65. $stmt->bind_result($sum1, $permission,$name);
  66. while ($stmt->fetch()){
  67. $row[] = array('sum1' => $sum1, 'permission_id' => $permission, 'name' => $name);
  68. }
  69. $stmt->close();
  70. return ($row);
  71. }
  72.  
  73.  
  74. // Usage eg index.php -- *** NOTE revised footer structure
  75. // also need some CSS
  76.  
  77. #flotcontainer {
  78. height: 400px;
  79. text-align: left;
  80. }
  81.  
  82. <div id="legendPlaceholder"></div>
  83. <div id="flotcontainer"></div>
  84.  
  85. <!-- footers -->
  86. <?php require_once("models/page_footer.php"); // the final html footer copyright row + the external js calls ?>
  87.  
  88. <!-- Flot Charts JavaScript -->
  89. <!--[if lte IE 8]><script src="js/excanvas.min.js"></script><![endif]-->
  90. <script type="text/javascript" src="js/plugins/flot/jquery.flot.js"></script>
  91. <script type="text/javascript" src="js/plugins/flot/jquery.flot.tooltip.min.js"></script>
  92. <script type="text/javascript" src="js/plugins/flot/jquery.flot.resize.js"></script>
  93. <script type="text/javascript" src="js/plugins/flot/jquery.flot.pie.js"></script>
  94.  
  95. <script type="text/javascript">
  96. $(document).ready(function(){
  97. $.ajax({
  98. url: 'json_chart.php',
  99. contentType: "application/json; charset=utf-8",
  100. type: "GET",
  101. dataType: 'json',
  102. success: function (data) {
  103. $.plot($("#flotcontainer"), data, {
  104. series: {
  105. pie: {
  106. show: true
  107. }
  108. },
  109. legend: {
  110. show: false
  111. }
  112. });
  113. },
  114. failure: function (response) {
  115. alert(response.d);
  116. }
  117.  
  118. });
  119.  
  120. });
  121. </script>
  122.  
  123. <?php require_once("models/html_footer.php"); // currently just the closing /body and /html ?>
  124.  
  125.  
  126. // Check permissions for the json_chart.php file
  127.  
  128. // finished!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement