File tree Expand file tree Collapse file tree 4 files changed +57
-0
lines changed Expand file tree Collapse file tree 4 files changed +57
-0
lines changed Original file line number Diff line number Diff line change
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
+ ?>
Original file line number Diff line number Diff line change 4
4
<body>
5
5
<header>
6
6
<a href="/php_mvc_blog">Home</a>
7
+ <a href='?controller=posts&action=index'>Posts</a>
7
8
8
9
</header>
9
10
<?php require_once ('routes.php ' ); ?>
Original file line number Diff line number Diff line change
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
+ ?>
Original file line number Diff line number Diff line change
1
+ <p>This is the requested post: </p>
2
+ <p><?php echo $ post ->author ; ?> </p>
3
+ <p><?php echo $ post ->content ; ?> </p>
You can’t perform that action at this time.
0 commit comments