Skip to content

Instantly share code, notes, and snippets.

@supanadit
Created August 1, 2019 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save supanadit/799728a9db6b2939c6ea38b01fb7feb9 to your computer and use it in GitHub Desktop.
Save supanadit/799728a9db6b2939c6ea38b01fb7feb9 to your computer and use it in GitHub Desktop.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Api extends CI_Controller
{
public function list_employee()
{
header('Content-Type: application/json');
$query = $this->db->get('employee');
if ($query) {
$response = array(
"data" => $query->result(),
"message" => "OK",
);
} else {
$response = array(
"data" => array(),
"message" => "Table Empty"
);
}
echo json_encode($response);
}
public function add_employee()
{
header('Content-Type: application/json');
$input = file_get_contents("php://input");
if (!empty($input) && isset($input)) {
$json = json_decode($input);
if (!empty($json->employee_name)) {
$data = array(
'name' => $json->employee_name,
);
$query = $this->db->insert('employee', $data);
if ($query) {
$response = array(
"data" => array(),
"message" => "Success Adding new Employee",
);
} else {
$response = array(
"data" => array(),
"message" => "Failed Adding new Employee",
);
}
} else {
$response = array(
"data" => array(),
"message" => "Employee Name Cannot be null",
);
}
} else {
$response = array(
"data" => array(),
"message" => "Body cannot be empty",
);
}
echo json_encode($response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment