Skip to content

Commit 1701f63

Browse files
initial commit
0 parents  commit 1701f63

13 files changed

+332
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Php Variables Files
2+
env.php

.htaccess

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RewriteEngine on
2+
RewriteRule ^([a-z]+)$ index.php?table=$1
3+
RewriteRule ^([a-z]+)/([0-9]+)$ index.php?table=$1&&id=$2

controllers/method.controller.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/* Method Controller Class */
4+
class MethodController {
5+
6+
function __construct() { }
7+
8+
/* Check Method Class */
9+
function MethodController($method, $table, $id) {
10+
switch ($method) {
11+
12+
/* GET Method */
13+
case "GET":
14+
15+
/* Include GET Model File */
16+
include 'models/get.model.php' ;
17+
$get = new GetModel();
18+
$get->getData($table, $id);
19+
break;
20+
21+
/* POST Method */
22+
case "POST":
23+
24+
/* Recive POST Data */
25+
$data = file_get_contents('php://input');
26+
27+
/* Include POST Model File */
28+
include 'models/post.model.php' ;
29+
$post = new PostModel();
30+
$post->postData($table, $data);
31+
break;
32+
33+
/* PUT Method */
34+
case "PUT":
35+
36+
/* Recive PUT Data */
37+
$data = file_get_contents('php://input');
38+
39+
/* Include PUT Model File */
40+
include 'models/put.model.php' ;
41+
$put = new PutModel();
42+
$put->putData($table, $id, $data);
43+
break;
44+
45+
/* DELETE Method */
46+
case "DELETE":
47+
48+
/* Include PUT Model File */
49+
include 'models/delete.model.php' ;
50+
$delete = new DeleteModel();
51+
$delete->deleteData($table, $id);
52+
break;
53+
}
54+
}
55+
}
56+
57+
?>

env-simple.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
/* Database Variables */
4+
define('SERVER_NAME', '');
5+
define('USER_NAME', '');
6+
define('PASSWORD', '');
7+
define('DATABASE_NAME', '');
8+
9+
?>

index.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/* Include View Files */
4+
include 'views/header.view.php';
5+
include 'views/table.view.php';
6+
7+
/* Check ID */
8+
if(!isset($_GET['id'])) { $id = null; }
9+
else { $id = $_GET['id']; }
10+
11+
/* Method Controller File */
12+
include 'controllers/method.controller.php';
13+
$method = new MethodController();
14+
$method->MethodController($_SERVER['REQUEST_METHOD'], $table , $id);
15+
16+
?>

models/delete.model.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/* Delete Class */
4+
class DeleteModel {
5+
6+
function __construct() { }
7+
8+
/* DELETE Data */
9+
function deleteData($table, $id) {
10+
11+
/* Check ID */
12+
if(!isset($id)) {
13+
echo json_encode(array('status'=>'Fail', 'error'=>'Please provide valid input.'));
14+
die();
15+
}
16+
17+
/* Include Database File */
18+
include 'views/database.view.php';
19+
20+
/* Delete Data */
21+
$sql = "DELETE FROM $table WHERE id= $id";
22+
23+
if ($con->query($sql) === TRUE) {
24+
echo json_encode(array('status'=>'Success', 'message'=>'Data is Deleted.'));
25+
}
26+
else {
27+
echo json_encode(array('status'=>'Fail', 'error'=>'Please provide valid input.'));
28+
die();
29+
}
30+
}
31+
}

models/get.model.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/* GET Class */
4+
class GetModel {
5+
6+
function __construct() { }
7+
8+
/* GET All Data */
9+
function getData($table, $id) {
10+
11+
/* Check ID */
12+
if(!isset($id)) { $where = ''; }
13+
else { $where = "WHERE id = $id"; }
14+
15+
/* Include Database File */
16+
include 'views/database.view.php';
17+
18+
/* Get All Data */
19+
$sql = "SELECT * FROM $table $where";
20+
$result = mysqli_query($con, $sql);
21+
22+
if (mysqli_num_rows($result) > 0) {
23+
echo json_encode(array('status'=>'Success', 'data'=>mysqli_fetch_all($result, MYSQLI_ASSOC)));
24+
}
25+
else {
26+
echo json_encode(array('status'=>'Fail', 'error'=>'Please provide valid input.'));
27+
die();
28+
}
29+
}
30+
}

models/post.model.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/* POST Class */
4+
class PostModel {
5+
6+
function __construct() { }
7+
8+
/* POST Data */
9+
function postData($table, $data) {
10+
11+
/* Check Data */
12+
if(!isset($data)) {
13+
echo json_encode(array('status'=>'Fail', 'error'=>'Please provide valid input.'));
14+
die();
15+
}
16+
17+
/* Include Database File */
18+
include 'views/database.view.php';
19+
20+
/* Insert Data */
21+
$data = json_decode($data, true);
22+
$keys = implode(",", array_keys($data));
23+
$vals = implode("','", array_values($data));
24+
25+
$sql = "INSERT INTO $table ($keys) VALUES ('$vals')";
26+
27+
if ($con->query($sql) === TRUE) {
28+
echo json_encode(array('status'=>'Success', 'message'=>'Data is Inserted.'));
29+
}
30+
else {
31+
echo json_encode(array('status'=>'Fail', 'error'=>'Please provide valid input.'));
32+
die();
33+
}
34+
}
35+
}

models/put.model.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/* PUT Class */
4+
class PutModel {
5+
6+
function __construct() { }
7+
8+
/* PUT Data */
9+
function putData($table, $id, $data) {
10+
11+
/* Check Data and Check ID */
12+
if(!isset($data) && !isset($id)) {
13+
echo json_encode(array('status'=>'Fail', 'error'=>'Please provide valid input.'));
14+
die();
15+
}
16+
17+
/* Include Database File */
18+
include 'views/database.view.php';
19+
20+
/* Update Data */
21+
$data = json_decode($data, true);
22+
$dataString = '';
23+
24+
foreach($data as $key=>$value) {
25+
$dataString = $dataString."$key = '$value', ";
26+
}
27+
28+
$dataString = substr($dataString, 0, -2);
29+
30+
$sql = "UPDATE $table SET $dataString WHERE id = $id";
31+
32+
if ($con->query($sql) === TRUE) {
33+
echo json_encode(array('status'=>'Success', 'message'=>'Data is Updated.'));
34+
}
35+
else {
36+
echo json_encode(array('status'=>'Fail', 'error'=>'Please provide valid input.'));
37+
die();
38+
}
39+
}
40+
}

php-rest-api.sql

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
-- phpMyAdmin SQL Dump
2+
-- version 4.9.0.1
3+
-- https://www.phpmyadmin.net/
4+
--
5+
-- Host: 127.0.0.1
6+
-- Generation Time: Jun 27, 2023 at 07:21 PM
7+
-- Server version: 10.3.16-MariaDB
8+
-- PHP Version: 7.3.6
9+
10+
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
11+
SET AUTOCOMMIT = 0;
12+
START TRANSACTION;
13+
SET time_zone = "+00:00";
14+
15+
16+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
17+
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
18+
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
19+
/*!40101 SET NAMES utf8mb4 */;
20+
21+
--
22+
-- Database: `php-rest-api`
23+
--
24+
25+
-- --------------------------------------------------------
26+
27+
--
28+
-- Table structure for table `test`
29+
--
30+
31+
CREATE TABLE `test` (
32+
`id` int(11) NOT NULL,
33+
`name` varchar(100) NOT NULL,
34+
`email` varchar(100) NOT NULL,
35+
`class` varchar(100) NOT NULL
36+
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
37+
38+
--
39+
-- Dumping data for table `test`
40+
--
41+
42+
INSERT INTO `test` (`id`, `name`, `email`, `class`) VALUES
43+
(1, 'Pramod Singh', 'spramodgusian@gmail.com', 'Xth'),
44+
(2, 'Arjub Singh', 'arjun@gmail.com', '4th'),
45+
(4, 'Pramod Singh', 'spramodgusian@gmail.com', 'Xth'),
46+
(6, 'Arjub Singh', 'spramodgusian@gmail.com', 'Xth'),
47+
(7, 'Arjub Singh', 'spramousian@gmail.com', 'Xth'),
48+
(8, 'Arjub Singh', 'spramousian@gmail.com', 'Xth');
49+
50+
--
51+
-- Indexes for dumped tables
52+
--
53+
54+
--
55+
-- Indexes for table `test`
56+
--
57+
ALTER TABLE `test`
58+
ADD PRIMARY KEY (`id`);
59+
60+
--
61+
-- AUTO_INCREMENT for dumped tables
62+
--
63+
64+
--
65+
-- AUTO_INCREMENT for table `test`
66+
--
67+
ALTER TABLE `test`
68+
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;
69+
COMMIT;
70+
71+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
72+
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
73+
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

0 commit comments

Comments
 (0)