From 093d5adf5c5e060b54215c3b45e4e433fb51b989 Mon Sep 17 00:00:00 2001 From: bastien ollier Date: Fri, 25 Nov 2022 13:38:22 +0100 Subject: [PATCH] mon code tp --- admins.php | 25 +++++++++++++++++++++++++ article.php | 13 +++++++++++++ connection.php | 32 ++++++++++++++++++++++++++++++++ gatewayAdmins.php | 23 +++++++++++++++++++++++ gatewayNews.php | 34 ++++++++++++++++++++++++++++++++++ index.php | 44 ++++++++++++++++++++++++++++++++++++++++++++ listeNews.php | 13 +++++++++++++ news.php | 37 +++++++++++++++++++++++++++++++++++++ 8 files changed, 221 insertions(+) create mode 100644 admins.php create mode 100644 article.php create mode 100644 connection.php create mode 100644 gatewayAdmins.php create mode 100644 gatewayNews.php create mode 100644 index.php create mode 100644 listeNews.php create mode 100644 news.php diff --git a/admins.php b/admins.php new file mode 100644 index 0000000..cd375db --- /dev/null +++ b/admins.php @@ -0,0 +1,25 @@ +username = $username; + $this->password = $password; + } + + public function getUsername() + { + return $this->username; + } + + public function getPassword() + { + return $this->password; + } +} + +?> diff --git a/article.php b/article.php new file mode 100644 index 0000000..f008293 --- /dev/null +++ b/article.php @@ -0,0 +1,13 @@ +

+ +

+"; + echo $row['pubdate']; + echo "
"; + echo $row['link']; + echo "
"; + + +?> diff --git a/connection.php b/connection.php new file mode 100644 index 0000000..b344a8e --- /dev/null +++ b/connection.php @@ -0,0 +1,32 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } + + +/** * @param string $query + * @param array $parameters * + * @return bool Returns `true` on success, `false` otherwise +*/ + + public function executeQuery(string $query, array $parameters = []) : bool{ + $this->stmt = parent::prepare($query); + foreach ($parameters as $name => $value) { + $this->stmt->bindValue($name, $value[0], $value[1]); + } + + return $this->stmt->execute(); + } + + public function getResults() : array { + return $this->stmt->fetchall(); + + } +} + +?> diff --git a/gatewayAdmins.php b/gatewayAdmins.php new file mode 100644 index 0000000..1fdda53 --- /dev/null +++ b/gatewayAdmins.php @@ -0,0 +1,23 @@ +con = $con; + } + + public function addAdmin($admin) + { + $query = "insert into admin(username,password) values (:username,:password);"; + $this->con->executeQuery($query, array(':username' => array($admin->getUsername(), PDO::PARAM_STR), + ':password' => array(hash("sha256",$admin->getPassword()), PDO::PARAM_STR) + ) + ); + } + +} + +?> diff --git a/gatewayNews.php b/gatewayNews.php new file mode 100644 index 0000000..b11c7d5 --- /dev/null +++ b/gatewayNews.php @@ -0,0 +1,34 @@ +con = $con; + } + + public function addNews($news) + { + $query = "insert into news(title,description,pubdate,link) values (:title,:description,:pubdate,:link);"; + $this->con->executeQuery($query, array(':title' => array($news->getTitle(), PDO::PARAM_STR), + ':description' => array($news->getDescription(), PDO::PARAM_STR), + ':pubdate' => array($news->getPubdate(), PDO::PARAM_STR), + ':link' => array($news->getLink(), PDO::PARAM_STR) + ) + ); + } + + public function getNews() + { + $query = "SELECT * FROM news order by pubdate;"; + $this->con->executeQuery($query, array()); + $results=$this->con->getResults(); + Foreach ($results as $article){ + $listeNews[] = new News($article["title"],$article['description'],$article['pubdate'],$article['link']); + } + return $listeNews; + } +} + +?> diff --git a/index.php b/index.php new file mode 100644 index 0000000..bdfed25 --- /dev/null +++ b/index.php @@ -0,0 +1,44 @@ + + + + + +addNews(new News($title,$description,$pubdate,$link)); + $gateAdmin = new gatewayAdmin($con); + + $gateAdmin->addAdmin(new admin("toto","tata")); + + + /*$results=$gateNews->getNews(); + Foreach ($results as $new){ + print $new->getTitle()."
".$new->getDescription()."
".$new->getPubDate()."
".$new->getLink()."

"; + }*/ + require("listeNews.php"); + + + } + catch( PDOException $Exception ) { + echo 'erreur'; + echo $Exception->getMessage(); + } +?> + + + diff --git a/listeNews.php b/listeNews.php new file mode 100644 index 0000000..12e1de1 --- /dev/null +++ b/listeNews.php @@ -0,0 +1,13 @@ +getNews(); + Foreach ($results as $new){ + print $new->getTitle()."
"; + print $new->getDescription()."
"; + print $new->getPubDate()."
"; + print ""; + print $new->getLink()."
"; + print "
"; + } + +?> diff --git a/news.php b/news.php new file mode 100644 index 0000000..dacfb9b --- /dev/null +++ b/news.php @@ -0,0 +1,37 @@ +title = $title; + $this->description = $description; + $this->pubdate = $pubdate; + $this->link = $link; + } + + public function getTitle() + { + return $this->title; + } + + public function getDescription() + { + return $this->description; + } + + public function getPubdate() + { + return $this->pubdate; + } + + public function getLink() + { + return $this->link; + } +} +?>