Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 1 | <?php |
| 2 | class User{ |
| 3 | public $id; |
| 4 | public $nomcomplet; |
| 5 | public $curs; |
| 6 | public $grau; |
| 7 | public $quimata; |
| 8 | |
| 9 | public function nom() { |
| 10 | $noms = explode(" ", $this->nomcomplet); |
| 11 | return $noms[0]; |
| 12 | } |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 13 | |
| 14 | public function nomcurs() { |
| 15 | if ($this->curs == 1) return "1er"; |
| 16 | if ($this->curs == 2) return "2on"; |
| 17 | if ($this->curs == 3) return "3er"; |
| 18 | if ($this->curs == 4) return "4rt"; |
| 19 | } |
| 20 | |
| 21 | public function nomgrau() { |
| 22 | if ($this->grau == 0) return "MAT"; |
| 23 | if ($this->grau == 1) return "EST"; |
| 24 | if ($this->grau == 2) return "MAMME"; |
| 25 | } |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 26 | } |
| 27 | |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 28 | function query($query) { |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 29 | // Create connection |
Andreu | 2457e40 | 2019-09-22 00:52:41 +0200 | [diff] [blame] | 30 | $credentials = new Credentials(); |
| 31 | $conn = new mysqli($credentials->servername, $credentials->username, $credentials->password, $credentials->dbname); |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 32 | if ($conn->connect_error) die("Connection failed: " . $conn->connect_error); |
| 33 | $conn->set_charset("utf8"); |
| 34 | |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 35 | // Execute query and save result |
Andreu | 9950105 | 2019-09-22 20:09:08 +0200 | [diff] [blame] | 36 | $result = $conn->query($query); |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 37 | |
| 38 | // Close the connection |
| 39 | $conn->close(); |
| 40 | |
| 41 | // Return result of query |
| 42 | return $result; |
| 43 | } |
| 44 | |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 45 | function get_users($id = 0, $getAsObjects = true) { |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 46 | $users = []; |
| 47 | |
| 48 | // Prepare the query |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 49 | $query = "SELECT * FROM users"; |
| 50 | if ($id > 0) $query .= " WHERE id=".$id; |
| 51 | |
| 52 | // Fetch the information of the user |
Andreu | 20cbd1d | 2019-09-22 00:00:57 +0200 | [diff] [blame] | 53 | if ($result = query($query)) { |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 54 | while ($row = $result->fetch_row()) { |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 55 | if ($getAsObjects) { |
| 56 | $user = new User(); |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 57 | $user->id = (int)$row[0]; |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 58 | $user->nomcomplet = $row[1]; |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 59 | $user->curs = (int)$row[2]; |
| 60 | $user->grau = (int)$row[3]; |
| 61 | $user->quimata = (int)$row[4]; |
| 62 | $user->requested = (int)$row[5]; |
| 63 | $user->mort = (int)$row[6]; |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 64 | $user->md5password = $row[7]; |
| 65 | } else { |
| 66 | $user = []; |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 67 | $user["id"] = (int)$row[0]; |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 68 | $user["nomcomplet"] = $row[1]; |
Andreu | 4b2fbd9 | 2019-09-22 22:30:25 +0200 | [diff] [blame] | 69 | $user["curs"] = (int)$row[2]; |
| 70 | $user["grau"] = (int)$row[3]; |
| 71 | $user["quimata"] = (int)$row[4]; |
| 72 | $user["requested"] = (int)$row[5]; |
| 73 | $user["mort"] = (int)$row[6]; |
Andreu | dd6cfb4 | 2019-09-22 19:52:39 +0200 | [diff] [blame] | 74 | $user["md5password"] = $row[7]; |
| 75 | } |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 76 | |
| 77 | array_push($users, $user); |
| 78 | } |
| 79 | $result->close(); |
| 80 | } else { |
Andreu | 2a26cd7 | 2019-09-22 01:58:23 +0200 | [diff] [blame] | 81 | die("Query failed: " . $query); |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 82 | } |
Andreu | abbcb7e | 2019-09-21 18:22:14 +0200 | [diff] [blame] | 83 | |
| 84 | if ($id > 0) return $users[0]; |
| 85 | else return $users; |
| 86 | } |
| 87 | ?> |