Giter Site home page Giter Site logo

simple-json-db's Introduction

SIMPLE JSON DB

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.

INSTALLING

In order to install the db, you need to download json-db.class.php and put it wherever you want.

    //Require the file in your script
    require("../your-path/library/json-db.class.php");

INSTANTIATE THE DB

In order to connect to a json db or to create one, you need to run the following code:

    // Instantiate a db with the default name (db.json)
    $database = new DB();

This will assign to the variable $database the database 'db', as you haven't provided a custom db.

    // A db named custom.json
    $database2 = new DB("custom");

This one, though, will assign to the variable $database2 the database 'custom', provided as a paramether when you instantiate the class. Please note that if the custom database doesn't exists when you instantiate it, an empty db will bee created.

INSERT

In order to insert a new field inside the selected db, you need to run the following code:

    //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)
    $new_data = [
        "id" => 1,
        "name" => "John",
        "surname" => "Doe"
    ];
     $database->insert($new_data, $new_data['id']);

GET SINGLE

In case you need a single result based on the key, you need to run the following code:

    $result = $database->getSingle("1");

    print_r($result);

This will return a Json object, like this:

{
  "1": {
    "id": "1",
    "name": "John",
    "surname": "Doe"
  }
}

GET LIST

You might also decide to select more than one result, based on a query. The query is an array of keys with the relative values, something like this:

    $query = [
        "name" => "John",
        "surname" => "Doe"
    ];

With this query, I'm trying to select all the results whose name is 'John' and whose surname is 'Doe'. Now we need to run that query and get our results:

    //Show several results based on array query (in this case, all the fields with name: "John" and surname: "Doe")
    $result2 = $database3->getList($query);

    print_r($result2);

This will return a Json object, like this:

{
  "1": {
    "id": 1,
    "name": "John",
    "surname": "Doe",
    "age": 24,
    "city": "Amsterdam"
  },
  "27": {
    "id": 27,
    "name": "John",
    "surname": "Doe",
    "age": 47,
    "city": "Rome"
  }
}

This is just a test whith a database I've populated with several random results!

SORT

You can also sort your result by passing another param to the getList function, as it follows:

    //Order the provided param
    $result2 = $database3->getList($query, ["on" => "name", "order" => "ASC"]);

In the previous example, together with the function we have passed information about the way we want the result to be sorted:

  • on is the key we want to consider
  • order is the order, and it can be ASC or DESC

DELETE

You can easily delete a result by running the function delete, as it follows:

    //Remove the row from the db based on the key you pass
    $database3->delete("my-key");

CLEAR

You can easily clear the selected database by running the function clear, as it follows:

    //Clear the db
    $database3->clear();

EXAMPLE

You can have a look at the example (index.php) for more information about how to use Simple JSON DB.

Please, enjoy this class, and don't hesitate to ask, if you have any questions.

simple-json-db's People

Contributors

gventuri avatar simondotws avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.