diff --git a/connection.php b/connection.php index 37c9e4d..ba3e573 100644 --- a/connection.php +++ b/connection.php @@ -26,7 +26,8 @@ private function __clone() {} public static function getInstance() { if (!isset(self::$instance)) { $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; - self::$instance = new PDO('mysql:host=localhost;dbname=post', 'post', 'post', $pdo_options); + // self::$instance = new PDO('mysql:host=localhost;dbname=post', 'post', 'post', $pdo_options); + self::$instance = new PDO('mysql:host=localhost;dbname=post', 'root', '', $pdo_options); } return self::$instance; } diff --git a/controllers/posts_controller.php b/controllers/posts_controller.php index 8e2be0d..15e04dc 100644 --- a/controllers/posts_controller.php +++ b/controllers/posts_controller.php @@ -27,5 +27,61 @@ 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() + { + if(isset($_POST['add'])) + { + $first_name = $_POST['firstname']; + $last_name = $_POST['lastname']; + $author = $_POST['author']; + $content = $_POST['content']; + } + $arr = array( + 'first_name' =>$first_name, + 'last_name' =>$last_name, + 'author' => $author, + 'content' => $content); + $addPost = Post::add($arr); + } + + public function delete() + { + if(!isset($_GET['id'])) + return call('pages','error'); + $del = Post::del($_GET['id']); + } + + public function edit() + { + if (!isset($_GET['id'])) + return call('pages', 'error'); + $posts = Post::find($_GET['id']); + require_once('views/posts/edit.php'); + } + + public function doedit() + { + if(isset($_POST['edit'])) + { + $id = $_POST['id']; + $first_name = $_POST['firstname']; + $last_name = $_POST['lastname']; + $author = $_POST['author']; + $content = $_POST['content']; + } + $arr = array( + 'first_name' =>$first_name, + 'last_name' =>$last_name, + 'author' => $author, + 'content' => $content); + $editPost = Post::edit($arr,$id); + + } + } ?> \ No newline at end of file diff --git a/fasf.txt b/fasf.txt new file mode 100644 index 0000000..79e81bc --- /dev/null +++ b/fasf.txt @@ -0,0 +1 @@ +fdfdsgsdfg \ No newline at end of file diff --git a/models/post.php b/models/post.php index 5cd60b8..9d98c3a 100644 --- a/models/post.php +++ b/models/post.php @@ -51,7 +51,41 @@ public static function find($id) { $req->execute(array('id' => $id)); $post = $req->fetch(); - return new Post($post['id'], $post['author'], $post['content']); + return $post; + } + + public static function add($arr) { + $time = date('Y-m-d h:sa:i'); + $db = Db::getInstance(); + $req = "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($req); + if($query) { + echo "Add success!"; + } + } + + public static function del($id) { + $db = Db::getInstance(); + $id = intval($id); + $req = "DELETE FROM `posts` WHERE `id` = '".$id."'"; + $query = $db->query($req); + if($query) { + echo "Del success!"; + } + } + + public static function edit($arr,$id) { + $time = date('Y-m-d h:sa:i'); + $db = Db::getInstance(); + $id = intval($id); + $req = "UPDATE `posts` SET `first_name` = '".$_POST['firstname']."', `last_name` = '".$_POST['lastname']."', `author` = '".$_POST['author']."', `content` = '".$_POST['content']."', `created` = '".$time."', `modified` = '".$time."' WHERE `posts`.`id` = ".$id.""; + $query = $db -> query($req); + if($query) { + echo "edit success!"; + } + else + echo "error"; } } -?> \ No newline at end of file + +?> \ No newline at end of file diff --git a/routes.php b/routes.php index d0b5ab5..45f473c 100644 --- a/routes.php +++ b/routes.php @@ -18,7 +18,7 @@ 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', 'delete','edit','doedit']); /** @var string $controller */ if (array_key_exists($controller, $controllers)) { diff --git a/views/layout.php b/views/layout.php index 279fb4f..a6fadef 100644 --- a/views/layout.php +++ b/views/layout.php @@ -4,8 +4,9 @@
author; ?> See content + Delete + Edit
\ No newline at end of file diff --git a/views/posts/show.php b/views/posts/show.php index c9185e6..59c9853 100644 --- a/views/posts/show.php +++ b/views/posts/show.php @@ -1,4 +1,4 @@This is the requested post:
author; ?>
-content; ?>
\ No newline at end of file +content; ?>