├── README.md ├── controller ├── comment.php ├── page.php └── product.php ├── db.php ├── index.php ├── model ├── comment.php ├── page.php └── product.php ├── routes.php └── view ├── comment ├── add.php ├── delete.php ├── index.php └── showComments.php ├── footer.php ├── header.php ├── page └── error.php └── product ├── add.php ├── delete.php ├── index.php └── showProducts.php /README.md: -------------------------------------------------------------------------------- 1 | Simple-MVC-without-a-framework 2 | 3 | PHP + JS(JQUERY + AJAX) + CSS + HTML + RESTful + MySQL -> XAMP/UwAmp + PhpStorm 4 | 5 | This is a great simple MVC skeleton in PHP that implements all the basic features of web programming. 6 | 7 | I couldn't find a great MVC skeleton without all the functions implemented so I uploaded mine. 8 | 9 | The only file that can be confusing is routes.php that instead of having several different routes, it has just one that will get the name of the controller and the name of the action(function), therefore CRUD isn't effective. 10 | 11 | P.S - Don't forget to connect to your local database when running, just in case. 12 | Any questions, send me a message. 13 | 14 | -------------------------------------------------------------------------------- /controller/comment.php: -------------------------------------------------------------------------------- 1 | product_id; 39 | //execute the delete command on the model 40 | $comment=comment_model::delete($id); 41 | //return a simple view of confirming the deletion 42 | require_once("view/comment/delete.php"); 43 | } 44 | 45 | 46 | //add a comment 47 | function add(){ 48 | //read the data send over post method 49 | if(isset($_GET["product_id"])) { 50 | $nickname = $_POST["user"]; 51 | $message = $_POST["msg"]; 52 | $email = $_POST["email"]; 53 | $product_id = $_GET["product_id"]; 54 | //add a new comment using the model 55 | $comment = comment_model::add($nickname, $message, $email,$product_id); 56 | //return the conformation of succesful insertion 57 | require_once("view/comment/add.php"); 58 | } 59 | } 60 | 61 | 62 | } 63 | ?> -------------------------------------------------------------------------------- /controller/page.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /controller/product.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /db.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /model/comment.php: -------------------------------------------------------------------------------- 1 | id = $id; 13 | $this->nickname = $nickname; 14 | $this->message = $message; 15 | $this->email = $email; 16 | $this->product_id = $product_id; 17 | } 18 | 19 | 20 | public static function all(){ 21 | //list of al comments 22 | $list = []; 23 | $db = Db::getInstance(); 24 | $result = mysqli_query($db,'SELECT * FROM comment'); 25 | 26 | while($row = mysqli_fetch_assoc($result)){ 27 | $list[] = new comment_model($row['nickname'], $row['message'], $row['email'], $row['id'], $row['product_id']); 28 | } 29 | 30 | return $list; 31 | } 32 | 33 | public static function getComment($id){ 34 | $list = []; 35 | $db = Db::getInstance(); 36 | $result = mysqli_query($db,"SELECT * FROM comment where id = $id"); 37 | 38 | while($row = mysqli_fetch_assoc($result)){ 39 | $list = new comment_model( $row['nickname'], $row['message'], $row['email'], $row['id'], $row['product_id']); 40 | } 41 | return $list; 42 | } 43 | 44 | public static function allFromUser($product_id){ 45 | $list = []; 46 | $db = Db::getInstance(); 47 | if($result = mysqli_query($db,"SELECT * FROM comment where product_id = $product_id")) { 48 | while($row = mysqli_fetch_assoc($result)){ 49 | $list[] = new comment_model($row['nickname'], $row['message'], $row['email'], $row['id'], $row['product_id']); 50 | } 51 | } 52 | return $list; 53 | 54 | } 55 | 56 | 57 | public static function delete($id){ 58 | 59 | $db = Db::getInstance(); 60 | $result = mysqli_query($db,"delete from comment where id='$id'"); 61 | return true; 62 | 63 | } 64 | 65 | public static function add($nickname, $message, $email,$product_id) { 66 | 67 | $db = Db::getInstance(); 68 | $result = mysqli_query($db,"Insert into comment (nickname,message,email,product_id) Values ('$nickname','$message','$email','$product_id')"); 69 | $id=mysqli_insert_id($db); 70 | 71 | return new comment_model($nickname, $message, $email,$id,$product_id); 72 | } 73 | } 74 | ?> -------------------------------------------------------------------------------- /model/page.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /model/product.php: -------------------------------------------------------------------------------- 1 | id = $id; 10 | $this->name = $name; 11 | } 12 | 13 | public static function get($product_id){ 14 | $list = []; 15 | $db = Db::getInstance(); 16 | if($result = mysqli_query($db,"SELECT * FROM product where id = $product_id")) { 17 | if($row = mysqli_fetch_assoc($result)){ 18 | $list = new product_model($row['name'],$row['id']); 19 | } 20 | } 21 | 22 | 23 | 24 | return $list; 25 | } 26 | 27 | 28 | public static function all(){ 29 | //list of al comments 30 | $list = []; 31 | $db = Db::getInstance(); 32 | $result = mysqli_query($db,'SELECT * FROM product'); 33 | 34 | while($row = mysqli_fetch_assoc($result)){ 35 | $list[] = new product_model($row['name'],$row['id']); 36 | } 37 | 38 | return $list; 39 | } 40 | 41 | 42 | public static function delete($id){ 43 | $db = Db::getInstance(); 44 | $result = mysqli_query($db,"delete from product where id='$id'"); 45 | require_once("model/comment.php"); 46 | $result2 = mysqli_query($db,"delete from comment where product_id='$id'"); 47 | return true; 48 | } 49 | 50 | public static function add($name) { 51 | 52 | $db = Db::getInstance(); 53 | $result = mysqli_query($db,"Insert into product (name) Values ('$name')"); 54 | $id=mysqli_insert_id($db); 55 | return new product_model($name,$id); 56 | } 57 | } 58 | ?> -------------------------------------------------------------------------------- /routes.php: -------------------------------------------------------------------------------- 1 | {$action}(); 13 | } 14 | 15 | 16 | //an array, for the allowed controllers and their respective actions 17 | $controllers = array('product' => ['all','showAll','add','delete'], 18 | 'comment' => ['all','showAll','allFromUser','delete','add']); 19 | 20 | 21 | //we check, if the invoked action is part of our mvc code 22 | //without this check, a malicous product, could execute arbitrary code 23 | if (array_key_exists($controller, $controllers)) { 24 | if (in_array($action, $controllers[$controller])) { 25 | call($controller, $action); 26 | } else { 27 | call('errorController', 'error'); 28 | } 29 | } else { 30 | call('errorController', 'error'); 31 | } 32 | 33 | 34 | 35 | ?> -------------------------------------------------------------------------------- /view/comment/add.php: -------------------------------------------------------------------------------- 1 | 2 | Comment added"; 5 | ?> 6 |
7 |
8 |
9 | 17 |
18 |
19 |
20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /view/comment/delete.php: -------------------------------------------------------------------------------- 1 |  2 | Comment deleted"; 5 | 6 | /* 7 |
8 |
9 |
10 | 19 |
20 |
21 |
22 | 23 | */ 24 | 25 | json_encode($comment); 26 | include "view/footer.php"; 27 | ?> -------------------------------------------------------------------------------- /view/comment/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 |
6 |
7 | 8 | 9 |
10 |

Product's comments

11 |
12 |
13 |
14 | 27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
Add comment to this product
39 |
40 |
41 | 42 |
43 |
44 | 45 |
46 |
47 | 48 |
49 | 50 |
51 |
52 |
53 |
54 | 55 |
56 |

Comments

57 | 58 |
59 |
60 |
61 |
62 |
    63 | 64 |
  • 65 | 66 | nickname;?> 67 | 68 | | message;?> | 69 | 70 | 71 | 72 | 73 |
  • 74 | 75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 | 124 | -------------------------------------------------------------------------------- /view/comment/showComments.php: -------------------------------------------------------------------------------- 1 |
2 |
3 | 17 |
18 |
-------------------------------------------------------------------------------- /view/footer.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /view/header.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Hello, world! 13 | 14 | 15 | 16 | 25 | 26 |
27 |
28 |
29 |

Assignment 2 - Web Programming

30 |
31 |
32 |
33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /view/page/error.php: -------------------------------------------------------------------------------- 1 | 

There was an error!

-------------------------------------------------------------------------------- /view/product/add.php: -------------------------------------------------------------------------------- 1 | 2 | Product added"; 5 | json_encode($product); 6 | include "view/footer.php"; 7 | ?> -------------------------------------------------------------------------------- /view/product/delete.php: -------------------------------------------------------------------------------- 1 |  2 | Product deleted"; 5 | json_encode($product); 6 | include "view/footer.php"; 7 | ?> -------------------------------------------------------------------------------- /view/product/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
6 |
7 |
8 | 9 |
10 |

Add a new product

11 |
12 |
13 |
14 |
15 |
16 | 17 |
18 | 19 |
20 |
21 |
22 |
23 |
24 |

My products

25 |
26 |
27 |
28 |
29 | 44 |
45 |
46 |
47 | 48 | 49 | 50 | 93 | 94 |
95 |
96 |
97 |
98 |
99 | 100 | -------------------------------------------------------------------------------- /view/product/showProducts.php: -------------------------------------------------------------------------------- 1 | 2 |
3 |
4 | 19 |
20 |
21 | --------------------------------------------------------------------------------