Guest User

Untitled

a guest
Mar 11th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. <?php
  2. header('Content-type: application/json');
  3. $sort = null;
  4. $dir = 'asc';
  5. $sort_dir = SORT_ASC;
  6. if(strlen($_GET['sort']) > 0) {$sort = $_GET['sort'];}
  7. if((strlen($_GET['dir']) > 0) && ($_GET['dir'] == 'desc')) {$dir = 'desc';$sort_dir = SORT_DESC;}
  8. else {$dir = 'asc';$sort_dir = SORT_ASC;}
  9. returnData($sort, $dir, $sort_dir);
  10.  
  11.  
  12. function returnData($sort, $dir, $sort_dir) {
  13. $allRecords = initArray();
  14. echo $allRecords;
  15. if(!is_null($sort)) {
  16. foreach ($allRecords as $key => $row) {
  17. $sortByCol[$key] = $row[$sort];
  18. }
  19. if(count($sortByCol) > 0) {
  20. array_multisort($sortByCol, $sort_dir, $allRecords);
  21. }
  22. }
  23. $data = array();
  24. $lastIndex = count($allRecords);
  25. for($i=0; $i<($lastIndex); $i++) {
  26. $data[] = $allRecords[$i];
  27. }
  28. $returnValue = array( 'sort'=>$sort, 'dir'=>$dir,'records'=>$data);
  29. require_once('JSON.php');
  30. $json = new Services_JSON();
  31. echo ($json->encode($returnValue));
  32. }
  33. function live(){
  34. $username="root";
  35. $password="";
  36. $database="marketwatch";
  37. $sql="SELECT * FROM mw";
  38. $encodable = array();
  39. mysql_connect('localhost', $username, $password);
  40. mysql_select_db($database) or die("Unable to select database");
  41. $result = mysql_query($sql);
  42. while($obj = mysql_fetch_object($result))
  43. {$encodable[] = $obj;}
  44. return $encodable;
  45. }
  46. function initArray() {
  47. return array( live());
  48. }
  49.  
  50. ?>
Add Comment
Please, Sign In to add comment