Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header('Content-type: application/json');
- $sort = null;
- $dir = 'asc';
- $sort_dir = SORT_ASC;
- if(strlen($_GET['sort']) > 0) {$sort = $_GET['sort'];}
- if((strlen($_GET['dir']) > 0) && ($_GET['dir'] == 'desc')) {$dir = 'desc';$sort_dir = SORT_DESC;}
- else {$dir = 'asc';$sort_dir = SORT_ASC;}
- returnData($sort, $dir, $sort_dir);
- function returnData($sort, $dir, $sort_dir) {
- $allRecords = initArray();
- echo $allRecords;
- if(!is_null($sort)) {
- foreach ($allRecords as $key => $row) {
- $sortByCol[$key] = $row[$sort];
- }
- if(count($sortByCol) > 0) {
- array_multisort($sortByCol, $sort_dir, $allRecords);
- }
- }
- $data = array();
- $lastIndex = count($allRecords);
- for($i=0; $i<($lastIndex); $i++) {
- $data[] = $allRecords[$i];
- }
- $returnValue = array( 'sort'=>$sort, 'dir'=>$dir,'records'=>$data);
- require_once('JSON.php');
- $json = new Services_JSON();
- echo ($json->encode($returnValue));
- }
- function live(){
- $username="root";
- $password="";
- $database="marketwatch";
- $sql="SELECT * FROM mw";
- $encodable = array();
- mysql_connect('localhost', $username, $password);
- mysql_select_db($database) or die("Unable to select database");
- $result = mysql_query($sql);
- while($obj = mysql_fetch_object($result))
- {$encodable[] = $obj;}
- return $encodable;
- }
- function initArray() {
- return array( live());
- }
- ?>
Add Comment
Please, Sign In to add comment