Skip to content
This repository was archived by the owner on Jun 22, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
56 changes: 56 additions & 0 deletions controllers/posts_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

}
?>
1 change: 1 addition & 0 deletions fasf.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fdfdsgsdfg
38 changes: 36 additions & 2 deletions models/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
?>

?>
2 changes: 1 addition & 1 deletion routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
3 changes: 2 additions & 1 deletion views/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
</head>
<body>
<header>
<a href='/php_mvc'>Home</a>
<a href='?controller=pages&action=home'>Home</a>
<a href='?controller=posts&action=index'>Posts</a>
<a href='?controller=posts&action=add'>add</a>
</header>

<?php require_once('routes.php'); ?>
Expand Down
7 changes: 7 additions & 0 deletions views/posts/add.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<form action="?controller=posts&action=doadd" method="post" >
firstname: <input type="text" name="firstname">
lastname: <input type="text" name="lastname" >
author: <input type="text" name="author">
content: <input type="text" name="content">
<input type="submit" name="add" value="add">
</form>
8 changes: 8 additions & 0 deletions views/posts/edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<form action="?controller=posts&action=doedit" method="post" >
<input type="hidden" name="id" value="<?php echo $posts['id'] ?>">
<p> firstname: <input type="text" name="firstname" value="<?php echo $posts['first_name'] ?>"></p>
<p> lastname: <input type="text" name="lastname" value="<?php echo $posts['last_name'] ?>" ></p>
<p> author: <input type="text" name="author" value="<?php echo $posts['author'] ?>"> </p>
<p> content: <input type="text" name="content" value="<?php echo $posts['content'] ?>"></p>
<p> <input type="submit" name="edit" value="edit"> </p>
</form>
2 changes: 2 additions & 0 deletions views/posts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
<p>
<?php echo $post->author; ?>
<a href='?controller=posts&action=show&id=<?php echo $post->id; ?>'>See content</a>
<a href='?controller=posts&action=delete&id=<?php echo $post->id; ?>'>Delete</a>
<a href='?controller=posts&action=edit&id=<?php echo $post->id; ?>'>Edit</a>
</p>
<?php } ?>
2 changes: 1 addition & 1 deletion views/posts/show.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<p>This is the requested post:</p>

<p><?php echo $post->author; ?></p>
<p><?php echo $post->content; ?></p>
<p><?php echo $post->content; ?></p>