· 6 years ago · Oct 22, 2019, 08:18 AM
1CREATE TABLE IF NOT EXISTS `items` (
2 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
3 `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
4 `description` text COLLATE utf8_unicode_ci NOT NULL,
5 `created_at` timestamp NULL DEFAULT NULL,
6 `updated_at` timestamp NULL DEFAULT NULL,
7 PRIMARY KEY (`id`)
8) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=63 ;
9
10class:Item
11method:getItems
12
13<?php
14require 'db_config.php';
15$num_rec_per_page = 5;
16if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
17$start_from = ($page-1) * $num_rec_per_page;
18$sqlTotal = "SELECT * FROM items";
19$sql = "SELECT * FROM items Order By id desc LIMIT $start_from, $num_rec_per_page";
20$result = $mysqli->query($sql);
21 while($row = $result->fetch_assoc()){
22 $json[] = $row;
23 }
24 $data['data'] = $json;
25$result = mysqli_query($mysqli,$sqlTotal);
26$data['total'] = mysqli_num_rows($result);
27echo json_encode($data);
28?>
29
30method:createItem
31<?php
32require 'db_config.php';
33$post = $_POST;
34$sql = "INSERT INTO items (title,description)
35VALUES ('".$post['title']."','".$post['description']."')";
36$result = $mysqli->query($sql);
37$sql = "SELECT * FROM items Order by id desc LIMIT 1";
38$result = $mysqli->query($sql);
39$data = $result->fetch_assoc();
40echo json_encode($data);
41?>
42
43method:updateItem
44<?php
45require 'db_config.php';
46$id = $_POST["id"];
47$post = $_POST;
48$sql = "UPDATE items SET title = '".$post['title']."'
49,description = '".$post['description']."'
50WHERE id = '".$id."'";
51$result = $mysqli->query($sql);
52$sql = "SELECT * FROM items WHERE id = '".$id."'";
53$result = $mysqli->query($sql);
54$data = $result->fetch_assoc();
55echo json_encode($data);
56?>
57
58method:deleteItem
59<?php
60 require 'db_config.php';
61 $id = $_POST["id"];
62 $sql = "DELETE FROM items WHERE id = '".$id."'";
63 $result = $mysqli->query($sql);
64 echo json_encode([$id]);
65?>