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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
DEMO MVC PHP by QUANGND
23 changes: 3 additions & 20 deletions connection.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
<?php

/**
* Class Db
*/
class Db {

/**
* @var null
*/
class Db {
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=post', 'root', '', $pdo_options);
}
return self::$instance;
}
}
}
?>
66 changes: 66 additions & 0 deletions controllers/posts_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);


}
}
?>
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
require_once('connection.php');

if (isset($_GET['controller']) && isset($_GET['action'])) {
$controller = $_GET['controller'];
$action = $_GET['action'];
Expand Down
52 changes: 51 additions & 1 deletion models/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,58 @@ public static function find($id) {
// the query was prepared, now we replace :id with our actual $id value
$req->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;
}
}
?>
4 changes: 1 addition & 3 deletions post.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
-------------------------------------------------------

DROP TABLE IF EXISTS `posts`;

CREATE TABLE `posts` (
Expand All @@ -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;



Expand Down
7 changes: 2 additions & 5 deletions routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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
37 changes: 37 additions & 0 deletions views/posts/add.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html>
<head>
<title>Add</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<style type="text/css">
form{
width: 70%;
margin: 0 auto;
}
</style>
</head>
<body>
<form action="?controller=posts&action=doAdd" method="POST" role="form">
<legend>ADD</legend>
<div class="form-group">
<label for="firstname">First name</label><br></br>
<input type="text" name = "firstname" class="form-group" id="" placeholder="Input field"><br></br>

<label for="lastname">Last name</label><br></br>
<input type="text" name = "lastname" class="form-group" id="" placeholder="Input field"><br></br>

<label for="author">Author</label><br></br>
<input type="text" name="author" class="form-group" id="" placeholder="input field"><br></br>

<label for="content">Content</label><br></br>
<input type="text" name = "content" class="form-group" id="" placeholder="Input field"><br></br>

</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
<!-- jQuery -->
<script src="//code.jquery.com/jquery.js"></script>
<!-- Bootstrap JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</body>
</html>
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=update&id=<?php echo $post->id;?>'>Update</a>
<a href='?controller=posts&action=delete&id=<?php echo $post->id; ?>'>Delete</a>
</p>
<?php } ?>
39 changes: 39 additions & 0 deletions views/posts/update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html>
<head>
<title>Update</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<style type="text/css">
form{
width: 70%;
margin: 0 auto;
}
</style>
</head>
<body>

<form action="?controller=posts&action=exeUpdate" method="POST" role="form">
<legend>Update</legend>
<div class="form-group">
<input type="hidden" name="id" value="<?php echo $post['id'] ?>">
<label for="firstname">First name</label><br></br>
<input type="text" name = "firstname" value="<?php echo($post['first_name']) ?>" class="form-group" id="" placeholder="Input field"><br></br>

<label for="lastname">Last name</label><br></br>
<input type="text" name = "lastname" value="<?php echo($post['last_name']) ?>" class="form-group" id="" placeholder="Input field"><br></br>

<label for="author">Author</label><br></br>
<input type="text" name="author" value ="<?php echo($post['author']) ?>"class="form-group" id="" placeholder="input field"><br></br>

<label for="content">Content</label><br></br>
<input type="text" name = "content" value="<?php echo($post['content']) ?>" class="form-group" id="" placeholder="Input field"><br></br>

</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
<!-- jQuery -->
<script src="//code.jquery.com/jquery.js"></script>
<!-- Bootstrap JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</body>
</html>