Skip to content

Commit 674cc3b

Browse files
committed
commiting the 4 time.
1 parent c4a3a2f commit 674cc3b

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

models/post.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
class Post{
3+
public $id;
4+
public $author;
5+
public $content;
6+
7+
public function __construct($id, $author, $content){
8+
$this->id = $id;
9+
$this->author = $author;
10+
$this->content = $content;
11+
}
12+
13+
public static funtion all(){
14+
$list = [];
15+
$db = Db::getInstance();
16+
$req = $db->query('SELECT * FROM posts');
17+
18+
foreach($req->fetchAll() as $post){
19+
$list[] = new Post($post['id'], $post['author'], $post['content']);
20+
21+
}
22+
23+
return $list;
24+
}
25+
26+
public static function find ($id){
27+
$db = Db::getInstance();
28+
29+
$id = intval($id);
30+
31+
$req = $db->prepare('SELECT * FROM posts WHERE id = :id');
32+
33+
$req->execute(array('id'=>$id));
34+
$post = $req->fetch();
35+
36+
return new Post($post['id'], $post['author'], $post['content'])
37+
}
38+
}
39+
?>

views/layout.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<body>
55
<header>
66
<a href="/php_mvc_blog">Home</a>
7+
<a href='?controller=posts&action=index'>Posts</a>
78

89
</header>
910
<?php require_once('routes.php'); ?>

views/posts/index.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<p>Here is a list of all posts: </p>
2+
3+
<?php
4+
foreach ($posts as $post) { ?>
5+
<p>
6+
<?php echo $post->author; ?>
7+
<a href='?controller=posts&action=show&id=<?php echo %post->id;?>'>See content</a>
8+
9+
</p>
10+
11+
<?php
12+
# code...
13+
}
14+
?>

views/posts/show.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p>This is the requested post: </p>
2+
<p><?php echo $post->author; ?></p>
3+
<p><?php echo $post->content; ?></p>

0 commit comments

Comments
 (0)