Guest User

Untitled

a guest
Apr 14th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. <?php
  2.  
  3. header('Content-type: application/json');
  4.  
  5. $sort = null;
  6. $dir = 'asc';
  7. $sort_dir = SORT_ASC;
  8.  
  9. if (strlen($_GET['sort']) > 0) {
  10. $sort = $_GET['sort'];
  11. }
  12. if ((strlen($_GET['dir']) > 0) && ($_GET['dir'] == 'desc')) {
  13. $dir = 'desc';
  14. $sort_dir = SORT_DESC;
  15. } else {
  16. $dir = 'asc';
  17. $sort_dir = SORT_ASC;
  18. }
  19.  
  20. returnData($sort, $dir, $sort_dir);
  21.  
  22. function returnData($sort, $dir, $sort_dir) {
  23.  
  24. $allRecords = initArray();
  25. echo $allRecords;
  26.  
  27. if(!is_null($sort)) {
  28.  
  29. foreach ($allRecords as $key => $row) {
  30. $sortByCol[$key] = $row[$sort];
  31. }
  32.  
  33. if(count($sortByCol) > 0) {
  34. array_multisort($sortByCol, $sort_dir, $allRecords);
  35. }
  36.  
  37. }
  38.  
  39. $data = array();
  40. $lastIndex = count($allRecords);
  41. for ($i=0; $i < $lastIndex; $i++) {
  42. $data[] = $allRecords[$i];
  43. }
  44.  
  45. $returnValue = array(
  46. "sort" => $sort,
  47. "dir" => $dir,
  48. "records" => $data
  49. );
  50.  
  51. require_once('JSON.php');
  52.  
  53. $json = new Services_JSON();
  54. echo json_encode($returnValue);
  55.  
  56. }
  57.  
  58. function live(){
  59.  
  60. $username="root";
  61. $password="";
  62. $database="marketwatch";
  63.  
  64. mysql_connect('localhost', $username, $password);
  65. mysql_select_db($database) or die("Unable to select database");
  66.  
  67. $sql="SELECT * FROM mw";
  68. $result = mysql_query($sql);
  69.  
  70. $encodable = array();
  71. while ($obj = mysql_fetch_object($result)) {
  72. $encodable[] = $obj;
  73. }
  74.  
  75. return $encodable;
  76.  
  77. }
  78.  
  79. function initArray() {
  80.  
  81. return array(live());
  82.  
  83. }
  84.  
  85. ?>
Add Comment
Please, Sign In to add comment