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
18 changes: 2 additions & 16 deletions connection.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
<?php

/**
* Class Db
*/
class Db {

/**
* @var null
*/

private static $instance = NULL;

/**
* Db constructor.
*/
private function __construct() {}

/**
*
*/
private function __clone() {}

/**
* @return null|PDO
*/
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=posts', 'root', '', $pdo_options);
}
return self::$instance;
}
Expand Down
49 changes: 49 additions & 0 deletions controllers/posts_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,54 @@ 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()
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$author = $_POST['author'];
$content = $_POST['content'];
$arr = array(
'first_name' => $first_name,
'last_name' => $last_name,
'author' => $author,
'content' => $content
);
$post = Post::add($arr);
}

public function delete()
{
if (isset($_POST['id']))
$post = Post::delete($_POST['id']);
require_once('views/posts/delete.php');
}

public function update()
{
require_once('views/posts/update.php');
}
public function doUpdate()
{
if (isset($_POST['id']))
$id = $_POST['id'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$author = $_POST['author'];
$content = $_POST['content'];
$arr = array(
'id' => $id,
'first_name' => $first_name,
'last_name' => $last_name,
'author' => $author,
'content' => $content
);
$post = Post::update($array);
}
}

?>
66 changes: 54 additions & 12 deletions models/post.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
<?php

/**
* Class Post
*/
class Post {
// we define 3 attributes
// we define 5 attributes
// they are public so that we can access them using $post->author directly
public $id;
public $first_name;
public $last_name;
public $author;
public $content;
public $created;
public $modified;

/**
* Post constructor.
* @param $id
* @param $author
* @param $content
*/
public function __construct($id, $author, $content) {
$this->id = $id;
$this->id = $id;
$this->author = $author;
$this->content = $content;
}
Expand Down Expand Up @@ -53,5 +48,52 @@ public static function find($id) {

return new Post($post['id'], $post['author'], $post['content']);
}
}
public static function add($arr) {
$db = Db::getInstance();
$time = date("Y-m-d h:sa:i");
$sql = "INSERT INTO `posts` (`id`, `first_name`, `last_name`, `author`, `content`, `created`, `modified`) VALUES (NULL, '".$_POST['first_name']."', '".$_POST['last_name']."', '".$_POST['author']."', '".$_POST['content']."', '".$time."', '".$time."')";

$query = $db->query($sql);
if($query)
{
echo "Successful!";
}
else {
echo "Error!";
}
}

public static function delete($id) {
try
{
$db = Db::getInstance();
// we make sure $id is an integer
$id = intval($id);
$sql = "DELETE FROM `posts` WHERE id= $id";
$query = $db->exec($sql);
echo "Successful!";
}
catch(PDOException $e)
{
echo $query . "<br>" . $e->getMessage();
}
}

public static function update($array) {
try
{
$db = Db::getInstance();
// we make sure $id is an integer
$id = $_POST['id'];
$id = intval($id);
$sql = "UPDATE `posts` SET `first_name`='".$_POST['first_name']."','".`last_name`."'='".$_POST['last_name']."','".`author`."'='".$_POST['author']."','".`content`."'='".$_POST['content']."' WHERE id = $id ";
$query = $db->query($sql);
echo "Successful!";
}
catch(PDOException $e)
{
echo $query . "<br>" . $e->getMessage();
}
}
}
?>
1 change: 0 additions & 1 deletion post.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
-------------------------------------------------------

DROP TABLE IF EXISTS `posts`;

Expand Down
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', 'update', 'doUpdate']);

/** @var string $controller */
if (array_key_exists($controller, $controllers)) {
Expand Down
5 changes: 4 additions & 1 deletion views/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
</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>
<a href='?controller=posts&action=delete'>Delete</a>
<a href='?controller=posts&action=update'>Update</a>
</header>

<?php require_once('routes.php'); ?>
Expand Down
28 changes: 28 additions & 0 deletions views/posts/add.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<title>Them du lieu</title>
</head>
<body>
<br /> <br />
<form action="?controller=posts&action=doAdd" method="post">
<table>
<tr>
<th> first_name:</th>
<td> <input type="text" name="first_name" value=""> </td>
</tr>
<tr>
<th> last_name: </th> <td> <input type="text" name="last_name" value="" /> </td>
</tr>
<tr>
<th> author: </th> <td> <input type="text" name="author" value="" /> </td>
</tr>
<tr>
<th> content: </th> <td> <input type="text" name="content" value="" /> </td>
</tr>

</table>
<input type="submit" name="submit" value="add">
</form>
</body>
</html>
12 changes: 12 additions & 0 deletions views/posts/delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Xoa du lieu</title>
</head>
<body>
<br /> <br />
<form action="?controller=posts&action=delete" method="post">
ID: <input type="text" name="id" value = "" />
<input type="submit" name="submit" value="delete">
</body>
</html>
30 changes: 30 additions & 0 deletions views/posts/update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>
<head>
<title>Chinh sua du lieu</title>
</head>
<body>
<br /> <br />
<form action="?controller=posts&action=update" method="post">
<table>
<tr>
<th> first_name:</th>
<td> <input type="text" name="first_name" value=""> </td>
</tr>
<tr>
<th> last_name: </th> <td> <input type="text" name="last_name" value="" /> </td>
</tr>
<tr>
<th> author: </th> <td> <input type="text" name="author" value="" /> </td>
</tr>
<tr>
<th> content: </th> <td> <input type="text" name="content" value="" /> </td>
</tr>

</table>
<br /> <br />
ID: <input type="text" name = "ID" value="">
<input type="submit" name="submit" value="update">
</form>
</body>
</html>