progetto201 API
Functions | Variables
colors.php File Reference

COLORS: manages the colors table. More...

Functions

 get_colors ($t_conn_res)
 Returns the colors data inside an associative array (the key is called 'data'). More...
 

Variables

 $response = array('errors' => array())
 Default response array. More...
 

Detailed Description

COLORS: manages the colors table.

Client perspective:

Description

This script sets the content type to "JSON", includes the db_connection.php script

Which is used to connect to MySQL and to execute simple select queries (no prepared statements)

and then it defines the response array.

A function is defined:

get_colors($t_conn_res)

Returns colors data inside an associative array. The name of its key is 'data'.

If the script is not included in other scripts, the script tries to connect to the database, and checks the request method:

When the function finishes, the script closes the connection to the DB, and the data (or errors) is collected in the "response" array. The response array is then returned to the client in JSON format

List of possible errors:

Since
01_01
Author
Stefano Zenaro (https://github.com/mario33881)
Todo:

Check if n is numeric and positive (now returns all the colors if n<0)

Check if min and max are numeric and min < max (currently returns "Query Returned nothing" in these cases))

Function Documentation

◆ get_colors()

get_colors (   $t_conn_res)

Returns the colors data inside an associative array (the key is called 'data').

The function, using the mysqli object inside the $t_conn_res parameter ($t_conn_res['connect_obj']), executes the '$query' query that collects the colors requested by the client.

The returned data depends on which parameters were passed with the request:

  • if the request has an 'n' parameter: returns that number of colors
  • if the request has the 'min' and 'max' parameters: returns the colors with an id value between those values
  • otherwise: returns all the colors

The data of the each color is contained inside an associative array:

  • id: identification number
  • color_name: name of the color
  • color_hex: hexadecimal value of the color

    the format of color_hex is:

    #RRGGBB

If the query execution throws an error, the error is collected in the array with the 'errors' key. The errors have 3 properties:

  • id: identifies the error in the API
  • htmlcode: its the response status code
  • message: details about the error

    the error message is written in english

Examples:

  • Example with no errors and without parameters
    GET /api/colors/colors.php
    {
    "errors": [],
    "data": [{
    "id": "4384",
    "color_name": "red",
    "color_hex": "#f44336"
    },
    {
    "id": "4385",
    "color_name": "randomname",
    "color_hex": "#f44337"
    },
    ...
    ]
    }
  • Example with no errors and with the 'n' parameter:
    GET /api/colors/colors.php?n=3
    {
    "errors": [],
    "data": [
    {
    "id": 4379,
    "color_name": "red lighten-5",
    "color_hex": "#ffebee"
    },
    {
    "id": 4380,
    "color_name": "red lighten-4",
    "color_hex": "#ffcdd2"
    },
    {
    "id": 4381,
    "color_name": "red lighten-3",
    "color_hex": "#ef9a9a"
    }
    ]
    }
  • Example with no errors and with the 'min'/'max' parameter:
    GET /api/colors/colors.php?min=4385&max=4387
    {
    "errors": [],
    "data": [
    {
    "id": 4385,
    "color_name": "red darken-1",
    "color_hex": "#e53935"
    },
    {
    "id": 4386,
    "color_name": "red darken-2",
    "color_hex": "#d32f2f"
    },
    {
    "id": 4387,
    "color_name": "red darken-3",
    "color_hex": "#c62828"
    }
    ]
    }

If there are errors, they are collected in the returned array. The function can retun the following errors:

  • 310: Query returned nothing
  • 311: Error executing query
  • 312: If 'min' is set so needs to be 'max' (and viceversa)
Since
01_01
Parameters
array$t_conn_resarray with the connection object (connection is successfull)
Returns
array $action_res array with colors data (or error)

Variable Documentation

◆ $response

$response = array('errors' => array())

Default response array.