├── index.php ├── library └── json-db.class.php └── README.md /index.php: -------------------------------------------------------------------------------- 1 | 1, 12 | "name" => "John", 13 | "surname" => "Doe" 14 | ); 15 | // Add a new field to the db, passing the data (an array) and the key (in this case, the id) 16 | $db->insert($data, $data['id']); 17 | 18 | // Show one single result based on the key 19 | $result = $db->getSingle($data['id']); 20 | 21 | // Show several results based on array query (in this case, all the fields with name: "John" and surname: "Doe") 22 | $query = [ 23 | "name" => "John", 24 | "surname" => "Doe" 25 | ]; 26 | $result2 = $db->getList($query); 27 | 28 | // Remove the row from the db based on the key you pass 29 | $db->delete($data['id']); 30 | ?> -------------------------------------------------------------------------------- /library/json-db.class.php: -------------------------------------------------------------------------------- 1 | path = $path; 18 | 19 | if(!file_exists($path)){ 20 | // If the .json extension is not provided, append it 21 | if(strpos($path, '.json') === false){ $path .= '.json'; } 22 | 23 | $fp = fopen($path,"wb"); 24 | fwrite($fp, "{}"); 25 | fclose($fp); 26 | } 27 | 28 | // Get the contect of the current path 29 | $this->db = json_decode(file_get_contents($path), true); 30 | } 31 | 32 | /** 33 | * SAVE 34 | * Save the new db 35 | */ 36 | private function save(){ 37 | $json = ($this->db === "{}") ? $this->db : json_encode($this->db); 38 | 39 | file_put_contents($this->path, $json); 40 | } 41 | 42 | /** 43 | * INSERT 44 | * 45 | * @param $data: array 46 | * @param $key: string (optional) 47 | * 48 | * @return DB 49 | */ 50 | public function insert($data, $key = ""){ 51 | if($key !== "") 52 | $this->db[$key] = $data; 53 | else 54 | $this->db[] = $data; 55 | 56 | $this->save(); 57 | 58 | return $this; 59 | } 60 | 61 | /** 62 | * UPDATE 63 | * 64 | * @param $data: array 65 | * @param $key: string 66 | * 67 | * @return DB 68 | */ 69 | public function update($data, $key){ 70 | if($key !== "") 71 | $update = array_merge($this->db[$key], $data); 72 | $this->db[$key] = $update; 73 | 74 | $this->save(); 75 | 76 | return $this; 77 | } 78 | 79 | /** 80 | * DELETE 81 | * 82 | * @param $key: string 83 | * 84 | * @return DB 85 | */ 86 | public function delete($key){ 87 | unset($this->db[$key]); 88 | 89 | $this->save(); 90 | 91 | return $this; 92 | } 93 | 94 | /** 95 | * GET SINGLE 96 | * 97 | * @param $key: string 98 | * 99 | * @return array 100 | */ 101 | public function getSingle($key){ 102 | return $this->db[$key] ?? null; 103 | } 104 | 105 | /** 106 | * GET LIST 107 | * 108 | * @param $conditions: array (optional) 109 | * @param $orderBy: array (optional) 110 | * 111 | * @return array 112 | */ 113 | public function getList($conditions = [], $orderBy = []){ 114 | $result = []; 115 | 116 | if(empty($conditions)){ 117 | $result = $this->db; 118 | }else{ 119 | foreach($this->db as $key => $value){ 120 | $requirements = true; 121 | 122 | foreach($conditions as $k => $v){ 123 | if($value[$k] !== $v){ 124 | $requirements = false; 125 | } 126 | } 127 | 128 | if($requirements) $result[$key] = $value; 129 | } 130 | } 131 | 132 | if($orderBy['on'] !== '' && $orderBy['order'] !== ''){ 133 | usort($result, function($first, $second) use($orderBy){ 134 | if($orderBy['order'] === "ASC"){ 135 | return strcmp($first[$orderBy['on']], $second[$orderBy['on']]) > 0; 136 | }else{ 137 | return strcmp($first[$orderBy['on']], $second[$orderBy['on']]) < 0; 138 | } 139 | }); 140 | } 141 | 142 | return $result; 143 | } 144 | 145 | /** 146 | * CLEAR 147 | * 148 | * @return DB 149 | */ 150 | public function clear(){ 151 | $this->db = "{}"; 152 | 153 | $this->save(); 154 | 155 | return $this; 156 | } 157 | } 158 | ?> 159 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SIMPLE JSON DB 2 | 3 | A simple json db class, very useful to run some tests or to develop very basic apps for personal use on the fly. Also, it's very easy to configure. 4 | 5 | ## INSTALLING 6 | 7 | In order to install the db, you need to download `json-db.class.php` and put it wherever you want. 8 | 9 | ```php 10 | //Require the file in your script 11 | require("../your-path/library/json-db.class.php"); 12 | ``` 13 | 14 | ## INSTANTIATE THE DB 15 | 16 | In order to connect to a json db or to create one, you need to run the following code: 17 | 18 | ```php 19 | // Instantiate a db with the default name (db.json) 20 | $database = new DB(); 21 | ``` 22 | 23 | This will assign to the variable `$database` the database 'db', as you haven't provided a custom db. 24 | 25 | ```php 26 | // A db named custom.json 27 | $database2 = new DB("custom"); 28 | ``` 29 | 30 | This one, though, will assign to the variable `$database2` the database 'custom', provided as a paramether when you instantiate the class. 31 | Please note that if the custom database doesn't exists when you instantiate it, an empty db will bee created. 32 | 33 | ## INSERT 34 | 35 | In order to insert a new field inside the selected db, you need to run the following code: 36 | 37 | ```php 38 | //Add a new field to the db, passing the data (an array) and the key (in this case, the id, but you can choose a custom one) 39 | $new_data = [ 40 | "id" => 1, 41 | "name" => "John", 42 | "surname" => "Doe" 43 | ]; 44 | $database->insert($new_data, $new_data['id']); 45 | ``` 46 | 47 | ## GET SINGLE 48 | 49 | In case you need a single result based on the key, you need to run the following code: 50 | 51 | ```php 52 | $result = $database->getSingle("1"); 53 | 54 | print_r($result); 55 | ``` 56 | 57 | This will return a Json object, like this: 58 | 59 | ```json 60 | { 61 | "1": { 62 | "id": "1", 63 | "name": "John", 64 | "surname": "Doe" 65 | } 66 | } 67 | ``` 68 | 69 | ## GET LIST 70 | 71 | You might also decide to select more than one result, based on a query. 72 | The query is an array of keys with the relative values, something like this: 73 | 74 | ```php 75 | $query = [ 76 | "name" => "John", 77 | "surname" => "Doe" 78 | ]; 79 | ``` 80 | 81 | With this query, I'm trying to select all the results whose name is 'John' and whose surname is 'Doe'. 82 | Now we need to run that query and get our results: 83 | 84 | ```php 85 | //Show several results based on array query (in this case, all the fields with name: "John" and surname: "Doe") 86 | $result2 = $database3->getList($query); 87 | 88 | print_r($result2); 89 | ``` 90 | 91 | This will return a Json object, like this: 92 | 93 | ```json 94 | { 95 | "1": { 96 | "id": 1, 97 | "name": "John", 98 | "surname": "Doe", 99 | "age": 24, 100 | "city": "Amsterdam" 101 | }, 102 | "27": { 103 | "id": 27, 104 | "name": "John", 105 | "surname": "Doe", 106 | "age": 47, 107 | "city": "Rome" 108 | } 109 | } 110 | ``` 111 | 112 | This is just a test whith a database I've populated with several random results! 113 | 114 | ### SORT 115 | 116 | You can also sort your result by passing another param to the `getList` function, as it follows: 117 | 118 | ```php 119 | //Order the provided param 120 | $result2 = $database3->getList($query, ["on" => "name", "order" => "ASC"]); 121 | ``` 122 | 123 | In the previous example, together with the function we have passed information about the way we want the result to be sorted: 124 | 125 | - `on` is the key we want to consider 126 | - `order` is the order, and it can be ASC or DESC 127 | 128 | ## DELETE 129 | 130 | You can easily delete a result by running the function `delete`, as it follows: 131 | 132 | ```php 133 | //Remove the row from the db based on the key you pass 134 | $database3->delete("my-key"); 135 | ``` 136 | 137 | ## CLEAR 138 | 139 | You can easily clear the selected database by running the function `clear`, as it follows: 140 | 141 | ```php 142 | //Clear the db 143 | $database3->clear(); 144 | ``` 145 | 146 | ### EXAMPLE 147 | 148 | You can have a look at the example (index.php) for more information about how to use Simple JSON DB. 149 | 150 | Please, enjoy this class, and don't hesitate to ask, if you have any questions. 151 | --------------------------------------------------------------------------------