[4362] | 1 | <?php |
---|
| 2 | class dataItem |
---|
| 3 | { |
---|
| 4 | var $name; |
---|
| 5 | var $id; |
---|
| 6 | var $login; |
---|
| 7 | var $fullName; |
---|
| 8 | var $email; |
---|
| 9 | var $isAdmin; |
---|
| 10 | var $pwd; |
---|
| 11 | var $comment; |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | //Constructor |
---|
| 17 | function dataItem($id,$login,$fullName,$email,$isAdmin,$pwd,$comment) |
---|
| 18 | { |
---|
| 19 | $this->id = $id; |
---|
| 20 | $this->login = $login; |
---|
| 21 | $this->fullName = $fullName; |
---|
| 22 | $this->email = $email; |
---|
| 23 | $this->isAdmin = $isAdmin; |
---|
| 24 | $this->pwd = $pwd; |
---|
| 25 | $this->comment = $comment; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | function getID() { return $this->id; } |
---|
| 30 | |
---|
| 31 | function getLogin() { return $this->login; } |
---|
| 32 | |
---|
| 33 | function getFullName() { return $this->fullName; } |
---|
| 34 | function getPwd() { return $this->pwd; } |
---|
| 35 | |
---|
| 36 | function getEmail() { return $this->email; } |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | function getComment() { return $this->comment; } |
---|
| 40 | function getIsAdmin() { return $this->isAdmin; } |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | class collection |
---|
| 47 | { |
---|
| 48 | var $dataSet = array(); |
---|
| 49 | |
---|
| 50 | //Creates a new data item and adds it to our array |
---|
| 51 | function add($id,$login,$fullName,$email,$isAdmin,$pwd,$comment) |
---|
| 52 | { |
---|
| 53 | $this->dataSet[] = new dataItem($id,$login,$fullName,$email,$isAdmin,$pwd,$comment); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | //The wrapper sort function |
---|
| 57 | function sortDataSet($s) |
---|
| 58 | { |
---|
| 59 | //Sort by the given parameter |
---|
| 60 | switch($s) |
---|
| 61 | { |
---|
| 62 | case "fullName": |
---|
| 63 | //Note use of array to reference member method of this object in callback |
---|
| 64 | uasort($this->dataSet,array($this,"cmpName")); |
---|
| 65 | break; |
---|
| 66 | |
---|
| 67 | case "login": |
---|
| 68 | uasort($this->dataSet,array($this,"cmpX")); |
---|
| 69 | break; |
---|
| 70 | |
---|
| 71 | case "id": |
---|
| 72 | uasort($this->dataSet,array($this,"cmpY")); |
---|
| 73 | break; |
---|
| 74 | |
---|
| 75 | case "added": |
---|
| 76 | default: |
---|
| 77 | //Re-sort array by original keys |
---|
| 78 | ksort($this->dataSet); |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | //Callback function for sorting by name |
---|
| 83 | //$a and $b are dataItem objects |
---|
| 84 | function cmpName($a,$b) |
---|
| 85 | { |
---|
| 86 | //Use sort() for simple alphabetical comparison |
---|
| 87 | //Convert to lowercase to ensure consistent behaviour |
---|
| 88 | $sortable = array(strtolower($a->fullName),strtolower($b->fullName)); |
---|
| 89 | $sorted = $sortable; |
---|
| 90 | sort($sorted); |
---|
| 91 | |
---|
| 92 | //If the names have switched position, return -1. Otherwise, return 1. |
---|
| 93 | return ($sorted[0] == $sortable[0]) ? -1 : 1; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | //Callback function for sorting by x |
---|
| 97 | //$a and $b are dataItem objects |
---|
| 98 | function cmpX($a,$b) |
---|
| 99 | { |
---|
| 100 | //Use sort() for simple alphabetical comparison |
---|
| 101 | //Convert to lowercase to ensure consistent behaviour |
---|
| 102 | $sortable = array(strtolower($a->x),strtolower($b->x)); |
---|
| 103 | $sorted = $sortable; |
---|
| 104 | sort($sorted); |
---|
| 105 | |
---|
| 106 | //If the names have switched position, return -1. Otherwise, return 1. |
---|
| 107 | return ($sorted[0] == $sortable[0]) ? -1 : 1; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | //Callback function for sorting by y |
---|
| 111 | //$a and $b are dataItem objects |
---|
| 112 | function cmpY($a,$b) |
---|
| 113 | { |
---|
| 114 | //If $a's y attribute >= $b's y attribute, return 1. Otherwise, return -1. |
---|
| 115 | return ($a->y >= $b->y) ? 1 : -1; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | } |
---|
| 119 | ?> |
---|