Controller:-Api.php
<?php
require(APPPATH.'/libraries/REST_Controller.php');
class Api extends REST_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('data_model');
}
function sdata_get()
{
$result =$this->data_model->all_data();
if($result)
{
$this->response($result, 200);
}
else
{
$this->response("No record found", 404);
}
return json_encode($result);
}
function idata_post()
{
$name = $this->post('name');
$description = $this->post('description');
$startdate = $this->post('startdate');
$enddate = $this->post('enddate');
if(!$name || !$description|| !$startdate|| !$enddate){
$this->response("Enter Your Full Name", 400);
}
else
{
$result = $this->data_model->add(array("name"=>$name, "description"=>$description, "startdate"=>$startdate, "enddate"=>$enddate));
if($result === 0){
$this->response("data insertion failed.", 404);
}
else
{
$this->response("success", 200);
}
}
}
//update
function udata_put()
{
$firstname= $this->put('firstname');
$lastname= $this->put('lastname');
$id = $this->put('id');
if(!$firstname || !$lastname ){
$this->response("data updated", 400);
}
else
{
$result = $this->data_model->update($id, array("firstname"=>$firstname, "lastname"=>$lastname));
if($result === 0){
$this->response("updation failed", 404);
}
else
{
$this->response("success", 200);
}
}
}
public function ddata_delete($id)
{
$id = $this->delete('id');
echo var_dump($id);
if(!$id){
$this->response("id not found", 404);
}
if($this->data_model->delete($id))
{
$this->response("success", 200);
}
else
{
$this->response("failed", 400);
}
}
function eventss_get(){
$result =$this->data_model->byid_data();
if($result)
{
$this->response($result, 200);
}
else
{
$this->response("No record found", 404);
}
return json_encode($result);
}
}
?>
Model:-
<?php
class Data_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function all_data(){
$q=$this->db->query('select * from event');
if($q->num_rows() > 0){
return $q->result_array();
}
else
{
return 0;
}
}
public function delete($id){
$this->db->where('id', $id);
if($this->db->delete('event')){
return true;
}else{
return false;
}
}
public function add($data){
if($this->db->insert('event', $data)){
return true;
}else{
return false;
}
}
public function update($id, $data){
$this->db->where('id', $id);
if($this->db->update('event', $data)){
return true;
}else{
return false;
}
}
public function byid_data($id){
$q=$this->db->query('select * from event where id=$id');
if($q->num_rows() > 0){
return $q->result_array();
}
else
{
return 0;
}
}
}
<?php
require(APPPATH.'/libraries/REST_Controller.php');
class Api extends REST_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('data_model');
}
function sdata_get()
{
$result =$this->data_model->all_data();
if($result)
{
$this->response($result, 200);
}
else
{
$this->response("No record found", 404);
}
return json_encode($result);
}
function idata_post()
{
$name = $this->post('name');
$description = $this->post('description');
$startdate = $this->post('startdate');
$enddate = $this->post('enddate');
if(!$name || !$description|| !$startdate|| !$enddate){
$this->response("Enter Your Full Name", 400);
}
else
{
$result = $this->data_model->add(array("name"=>$name, "description"=>$description, "startdate"=>$startdate, "enddate"=>$enddate));
if($result === 0){
$this->response("data insertion failed.", 404);
}
else
{
$this->response("success", 200);
}
}
}
//update
function udata_put()
{
$firstname= $this->put('firstname');
$lastname= $this->put('lastname');
$id = $this->put('id');
if(!$firstname || !$lastname ){
$this->response("data updated", 400);
}
else
{
$result = $this->data_model->update($id, array("firstname"=>$firstname, "lastname"=>$lastname));
if($result === 0){
$this->response("updation failed", 404);
}
else
{
$this->response("success", 200);
}
}
}
public function ddata_delete($id)
{
$id = $this->delete('id');
echo var_dump($id);
if(!$id){
$this->response("id not found", 404);
}
if($this->data_model->delete($id))
{
$this->response("success", 200);
}
else
{
$this->response("failed", 400);
}
}
function eventss_get(){
$result =$this->data_model->byid_data();
if($result)
{
$this->response($result, 200);
}
else
{
$this->response("No record found", 404);
}
return json_encode($result);
}
}
?>
Model:-
<?php
class Data_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function all_data(){
$q=$this->db->query('select * from event');
if($q->num_rows() > 0){
return $q->result_array();
}
else
{
return 0;
}
}
public function delete($id){
$this->db->where('id', $id);
if($this->db->delete('event')){
return true;
}else{
return false;
}
}
public function add($data){
if($this->db->insert('event', $data)){
return true;
}else{
return false;
}
}
public function update($id, $data){
$this->db->where('id', $id);
if($this->db->update('event', $data)){
return true;
}else{
return false;
}
}
public function byid_data($id){
$q=$this->db->query('select * from event where id=$id');
if($q->num_rows() > 0){
return $q->result_array();
}
else
{
return 0;
}
}
}
Comments
Post a Comment