class/Post.php
class/postManager.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 class Post { protected $_id; protected $_texte; protected $_dt_creation; public function __construct(array $data) { $this->setId($data['id']); $this->setText($data['texte']); $this->setDt_creation($data['dt_creation']); } public function setId($id) { if((is_int($id)) AND ($id > 0)) { $this->_id = $id; } } public function setText($texte) { if(is_string($texte)) { $this->_texte = $texte; } } public function setDt_creation($dt_creation) { list($y, $m, $d) = explode("-", $dt_creation); if(checkdate($m, $d, $y)) { $this->_dt_creation = $dt_creation; } } public function getId() { return $this->_id; } public function getText() { return $this->_texte; } public function getDt_creation() { return $this->_dt_creation; } } class Breve extends Post { } class Filet extends Post { protected $_title; public function __construct(array $data) { parent::__construct($data); $this->setTitle($data['title']); } public function setTitle($title) { if(is_string($title)) { $this->_title = $title; } } public function getTitle() { return $this->_title; } }
index.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 class postManager { private $_db; public function __construct($db) { $this->setDb($db); } public function setDb(PDO $dbh) { $this->__db = $dbh; } public function addPost(Post $post) { if(!method_exists($post, 'getTitle')) { $sql = 'INSERT INTO post1(texte, dt_creation) VALUES(:texte, :dt_creation)'; $stmnt = $this->__db->prepare($sql); } else { $sql = 'INSERT INTO post1(texte, dt_creation, title) VALUES(:texte, :dt_creation, :title)'; $stmnt = $this->__db->prepare($sql); } $stmnt->execute([':texte' => htmlspecialchars(($post)->getText()), ':dt_creation' => (($post)->getDt_creation()), ':title' => ($post)->getTitle()]); } public function getPost($id = '') { if(empty($id)) { $sql = 'SELECT id, texte, dt_creation, title FROM post1'; $stmnt = $this->__db->prepare($sql); } elseif(is_numeric($id)) { $sql = 'SELECT id, texte, dt_creation, title FROM post1 WHERE id = :id'; $stmnt = $this->__db->prepare($sql); $stmnt->bindParam(':id', $id); } $stmnt->execute(); while($row = $stmnt->fetch(PDO::FETCH_ASSOC)) { $result[] = $row; } return $result; }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 require('class/Post.php'); require('class/postManager.php'); $filet_data = array('id' => 1, 'texte' => 'Texte de breve', 'dt_creation' => '2013-11-15', 'title' => NULL); $filet = new Filet($filet_data); $db = new PDO('mysql:host=localhost;dbname=dbpost', 'root', ''); $manager = new postManager($db); $manager->addPost($filet); var_dump($filet);
Bonjour, Aucune données ne s'enregistre dans la base malgré qu'il y a aucune erreur d'afficher. Merci pour votre aide & bonne soirée
Partager