Created
August 27, 2012 07:16
-
-
Save tanqhnguyen/3486440 to your computer and use it in GitHub Desktop.
getAnnotatins.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Pagination details | |
$iDisplayStart = $_GET['iDisplayStart']; | |
$iDisplayLength = $_GET['iDisplayLength']; | |
// Table columns | |
$iColumns = $_GET['iColumns']; | |
$sColumns = $_GET['sColumns']; | |
$sColumns = explode(',', $sColumns); | |
// Sort details | |
$iSortingCols = $_GET['iSortingCols']; | |
$compareCriterias = array(); | |
for ($i = 0; $i < $iSortingCols; $i++) { | |
$index = $_GET['iSortCol_' . $i]; | |
$compareCriterias[$sColumns[$index]] = $_GET['sSortDir_' . $i]; | |
} | |
// Sample data | |
$data = array(); | |
$max = 1000; | |
for ($i = 1; $i <= $max; $i++) { | |
$number = $i; | |
if ($number < 10) { | |
$number = '00' . $number; | |
} else if ($number >= 10 && $number < 100) { | |
$number = '0' . $number; | |
} | |
if ($i % 2 == 0) { | |
$color = 'FFDE73'; | |
$tags = array('apple', 'asus'); | |
} else if ($i % 3 == 1) { | |
$color = 'E8BAFF'; | |
$tags = array('jobs'); | |
} else { | |
$color = 'C9E7FB'; | |
$tags = array('dell'); | |
} | |
array_push($data, array( | |
'id' => $i, | |
'content' => 'Quote ' . $number, | |
'note' => 'Note ' . $number, | |
'date' => 'date ' . $number, | |
'color' => $color, | |
'tags' => $tags | |
)); | |
} | |
// Sort | |
// http://stackoverflow.com/questions/5198276/php-multiple-uasort-functions-breaks-sorting | |
function make_comparer($criterias) { | |
$criteriaNames = array_keys($criterias); | |
$comparer = function($first, $second) use ($criteriaNames, $criterias) { | |
// Do we have anything to compare? | |
while(!empty($criteriaNames)) { | |
// What will we compare now? | |
$criterion = array_shift($criteriaNames); | |
if ($criterion == 'config') { | |
continue; | |
} | |
if (!is_array($first[$criterion])) { | |
if ($criterias[$criterion] == 'asc') { | |
return strcmp($first[$criterion], $second[$criterion]); | |
} | |
return -strcmp($first[$criterion], $second[$criterion]); | |
} else { | |
continue; | |
} | |
} | |
// Nothing more to compare with, so $first == $second | |
return 0; | |
}; | |
return $comparer; | |
} | |
usort($data, make_comparer($compareCriterias)); | |
// Paginate | |
$data = array_slice($data, $iDisplayStart, $iDisplayLength); | |
// Format the result | |
/* | |
$result = array(); | |
foreach ($data as $d) { | |
$row = array(); | |
foreach($sColumns as $col) { | |
if (isset($d[$col])) { | |
array_push($row, $d[$col]); | |
} else { | |
array_push($row, ''); | |
} | |
} | |
array_push($result, $row); | |
} | |
* | |
*/ | |
echo json_encode(array( | |
'error' => null, | |
'data' => array( | |
'sEcho' => $_GET['sEcho'], | |
'iTotalRecords' => $max, | |
'iTotalDisplayRecords' => $max, | |
'aaData' => $data | |
) | |
)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment