Skip to main content

RESTful web services in codeigniter

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;

        }


      }


}

Comments

Popular posts from this blog

Insert Fetch Update Delete Mysql data in Codeigniter

Database -- -- Table structure for table `tbl_user` -- CREATE TABLE IF NOT EXISTS `tbl_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `first_name` varchar(200) NOT NULL, `last_name` varchar(200) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=95 ; -- -- Dumping data for table `tbl_user` -- INSERT INTO `tbl_user` (`id`, `first_name`, `last_name`) VALUES (91, 'Harold', 'Jones'), (89, 'Christine', 'Smith'), (88, 'Marker', 'Angela'), (87, 'Romeo', 'Mary'), (86, 'Smith', 'John'); Controller - main.php <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Main extends CI_Controller { //functions public function index(){ $this->load->model("main_model"); $data["fetch_data"] = $this->main_model->...

How to Generate Excel File in Codeigniter using PHPExcel

Libraries/Excel.php <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require_once('PHPExcel.php'); class Excel extends PHPExcel { public function __construct() { parent::__construct(); } } ?> Libraries/IOFactory.php <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require_once('PHPExcel/IOFactory.php'); class IOFactory extends PHPExcel_IOFactory { public function __construct() { parent::__construct(); } } ?> Controllers/Excel_export.php <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Excel_export extends CI_Controller { function index() { $this->load->model("excel_export_model"); $data["employee_data"] = $this->excel_export_model->fetch_data(); $this->load->view("excel_export_view", $data); } function action() { $this->load->model(...

Core php login logout and register view delete

Core php login logout and register view delete Database.php CREATE TABLE IF NOT EXISTS register.`users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `email` varchar(50) NOT NULL, `password` varchar(50) NOT NULL, `trn_date` datetime NOT NULL, PRIMARY KEY (`id`) ); CREATE TABLE IF NOT EXISTS register.`new_record` ( `id` int(11) NOT NULL AUTO_INCREMENT, `trn_date` datetime NOT NULL, `name` varchar(50) NOT NULL, `age`int(11) NOT NULL, `submittedby` varchar(50) NOT NULL, PRIMARY KEY (`id`) ); Auth.php <?php session_start(); if(!isset($_SESSION["username"])){ header("Location: login.php"); exit(); } ?> Dashbaord .php <!DOCTYPE html> <html lang="en"> <head> <title>Dashboard</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.boo...