From 2dea3f07a8b4af42b346e5a49c8a2113d1fb1690 Mon Sep 17 00:00:00 2001 From: nhatthao Date: Mon, 18 Jun 2018 13:56:42 +0700 Subject: [PATCH] finished: added, deleted, updated of post --- README.md | 1 - connection.php | 23 ++--------- controllers/posts_controller.php | 66 ++++++++++++++++++++++++++++++++ index.php | 2 +- models/post.php | 52 ++++++++++++++++++++++++- post.sql | 4 +- routes.php | 7 +--- views/layout.php | 3 +- views/posts/add.php | 37 ++++++++++++++++++ views/posts/index.php | 2 + views/posts/update.php | 39 +++++++++++++++++++ 11 files changed, 204 insertions(+), 32 deletions(-) create mode 100644 views/posts/add.php create mode 100644 views/posts/update.php diff --git a/README.md b/README.md index d18ec7f..e69de29 100644 --- a/README.md +++ b/README.md @@ -1 +0,0 @@ -DEMO MVC PHP by QUANGND \ No newline at end of file diff --git a/connection.php b/connection.php index 37c9e4d..dcb1757 100644 --- a/connection.php +++ b/connection.php @@ -1,34 +1,17 @@ \ No newline at end of file diff --git a/controllers/posts_controller.php b/controllers/posts_controller.php index 8e2be0d..fda4054 100644 --- a/controllers/posts_controller.php +++ b/controllers/posts_controller.php @@ -27,5 +27,71 @@ public function show() { $post = Post::find($_GET['id']); require_once('views/posts/show.php'); } + + + public function add(){ + require_once('views/posts/add.php'); + + + } + public function doAdd() + { + $firstname= $_POST['firstname']; + $lastname=$_POST['lastname']; + $author=$_POST['author']; + $content=$_POST['content']; + + $arr = array( + 'first_name' => $firstname, + 'last_name'=>$lastname, + 'author'=>$author, + 'content'=>$content + + + ); + + require_once('models/post.php'); + $post = Post::add($arr); + } + public function update(){ + if (!isset($_GET['id'])) + return call('pages', 'error'); + + // we use the given id to get the right post + $post = Post::find($_GET['id']); + require_once('views/posts/update.php'); + } + + public function exeUpdate() + { + $id = $_POST['id']; + $firstname= $_POST['firstname']; + $lastname=$_POST['lastname']; + $author=$_POST['author']; + $content=$_POST['content']; + + $arr = array( + 'first_name' => $firstname, + 'last_name'=>$lastname, + 'author'=>$author, + 'content'=>$content + + + ); + + require_once('models/post.php'); + $post = Post::update($id,$arr); + } + + public function delete(){ + if (!isset($_GET['id'])) + return call('pages', 'error'); + + // we use the given id to get the right post + require_once('models/post.php'); + $post = Post::delete($_GET['id']); + + + } } ?> \ No newline at end of file diff --git a/index.php b/index.php index c3e7b18..c39f0f6 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,6 @@ execute(array('id' => $id)); $post = $req->fetch(); + return $post; + // return new Post($post['id'], $post['author'], $post['content']); + } + + public static function add($ad){ + + $time = date('Y-m-d h:sa:i'); + $db = Db::getInstance(); + $query = "INSERT INTO `posts` (`id`, `first_name`, `last_name`, `author`, `content`, `created`, `modified`) VALUES (NULL,'".$_POST['firstname']."', '".$_POST['lastname']."','".$_POST['author']."', '".$_POST['content']."','".$time."','".$time."') "; + $query= $db->query($query); + if($query) + { + echo "Added"; + } + else + { + echo "ERROR"; + } - return new Post($post['id'], $post['author'], $post['content']); + } + public static function update($id, $arr){ + $db = Db::getInstance(); + $id = intval($id); + $time = date('Y-m-d h:sa:i'); + $req = "UPDATE `posts` SET `first_name` = '".$_POST['firstname']."', `last_name` = '".$_POST['firstname']."', `author` = '".$_POST['firstname']."', `content` = '".$_POST['firstname']."', `created` = '".$time."', `modified` = '".$time."' WHERE `posts`.`id` = ".$id.""; + $query= $db->query($req); + if($query) + { + echo "updated"; + } + else + { + echo "ERROR"; + } + + } + + public static function delete($id){ + $db = Db::getInstance(); + $id = intval($id); + $req = "DELETE from posts WHERE id =".$id.""; + // $post = $req->fetch(); + $query= $db->query($req); + if($query) + { + echo "deleted"; + } + else + { + echo "ERROR"; + } + //return $post; } } ?> \ No newline at end of file diff --git a/post.sql b/post.sql index 1a616aa..fbc4962 100644 --- a/post.sql +++ b/post.sql @@ -1,5 +1,3 @@ -------------------------------------------------------- - DROP TABLE IF EXISTS `posts`; CREATE TABLE `posts` ( @@ -24,7 +22,7 @@ VALUES (4,'Quang desu','ok dsu','Fr-Vi','Fr-Vi','2018-04-25 04:03:13','2018-04-25 04:06:09'); /*!40000 ALTER TABLE `posts` ENABLE KEYS */; -UNLOCK TABLES; +/*UNLOCK TABLES; diff --git a/routes.php b/routes.php index d0b5ab5..a8cf807 100644 --- a/routes.php +++ b/routes.php @@ -18,12 +18,9 @@ function call($controller, $action) { // we're adding an entry for the new controller and its actions $controllers = array('pages' => ['home', 'error'], - 'posts' => ['index', 'show']); + 'posts' => ['index', 'show','add','doAdd','update','exeUpdate','delete']); -/** @var string $controller */ -if (array_key_exists($controller, $controllers)) { - - /** @var string $action */ + if (array_key_exists($controller, $controllers)) { if (in_array($action, $controllers[$controller])) { call($controller, $action); } else { diff --git a/views/layout.php b/views/layout.php index 279fb4f..e54b156 100644 --- a/views/layout.php +++ b/views/layout.php @@ -4,8 +4,9 @@
- Home + Home Posts + Add
diff --git a/views/posts/add.php b/views/posts/add.php new file mode 100644 index 0000000..e2d4df1 --- /dev/null +++ b/views/posts/add.php @@ -0,0 +1,37 @@ + + + + Add + + + + +
+ ADD +
+

+

+ +

+

+ +

+

+ +

+

+ +
+ +
+ + + + + + \ No newline at end of file diff --git a/views/posts/index.php b/views/posts/index.php index 23b47f4..46c2c4f 100644 --- a/views/posts/index.php +++ b/views/posts/index.php @@ -4,5 +4,7 @@

author; ?> See content + Update + Delete

\ No newline at end of file diff --git a/views/posts/update.php b/views/posts/update.php new file mode 100644 index 0000000..967c97d --- /dev/null +++ b/views/posts/update.php @@ -0,0 +1,39 @@ + + + + Update + + + + + +
+ Update +
+ +

+

+ +

+

+ +

+

+ +

+

+ +
+ +
+ + + + + + \ No newline at end of file