This is very simple php WebService that returns data in JSON format. I have created it to test my jQuery component, especially features like pagination and sort which are taken on server side.
The main goal of this webservice is to return a range of sorted data (page) eg:
Query params: Page size: 3 items Sort column: 1 Sort: ascending [ Data ] [Sort asc by col #1] [ Page #1 ] [ 1, 'Banana' ] [ 2, 'Apple' ] [ 2, 'Apple' ] [ 6, 'Avocado' ] [ 3, 'Pear' ] [ 1, 'Banana' ] [ 4, 'Orange' ] [ 9, 'Currants' ] [ 9, 'Currants' ] [ 5, 'Lemon' ] --> [ 5, 'Lemon' ] --> [ 5, 'Lemon' ] [ 6, 'Avocado' ] [ 4, 'Orange' ] [ 4, 'Orange' ] [ 7, 'Passionfruit'] [ 7, 'Passionfruit'] [ 8, 'Raspberries' ] [ 3, 'Pear' ] [ 9, 'Currants' ] [ 8, 'Raspberries' ] [ 10,'Strawberries'] [ 10,'Strawberries']
Below is example of URL request with query string:
http://host.com/webservice.php?page=0&pageSize=5&sort=0&sortOrder=asc
Where:
- page – is zero-based page number we want to get from server [eg 0].
- pageSize – is number of items on one page [eg 5].
- sort – is column index we want data to be sorted [eg 0].
- sortOrder – is sort order which takes the value of asc or desc.
PHP Code for WebService:
<?php $data = array( array( 1, "Banana"), array( 2, "Apple"), array( 3, "Pear"), array( 4, "Orange"), array( 5, "Lemon"), array( 6, "Avocado"), array( 7, "Passionfruit"), array( 8, "Raspberries"), array( 9, "Currants"), array( 10,"Strawberries") ); $page = is_null($_GET["page"]) ? 0 : $_GET["page"]; $pageSize = is_null($_GET["pageSize"]) ? 5 : $_GET["pageSize"]; $sort = is_null($_GET["sort"]) ? 0 : $_GET["sort"]; $sortOrder = is_null($_GET["sortOrder"]) ? 'asc': $_GET["sortOrder"]; $comparer = 'return ($a[$sort] == $b[$sort] ? 0 : ' . '($a[$sort] < $b[$sort] ? -1 : 1)) * ' . '($order=="asc"?1:-1);'; usort($data , create_function('$a,$b,$sort='.$sort.',$order='.$sortOrder , $comparer)); header('Content-type: application/json'); echo json_encode(array_slice($data, $page*$pageSize, $pageSize)); ?>
3 responses
Do you want to comment?
Comments RSS and TrackBack Identifier URI ?
Trackbacks