mon code tp

master
Bastien OLLIER 3 years ago
commit 093d5adf5c

@ -0,0 +1,25 @@
<?php
class Admin
{
private string $username;
private string $password;
public function __construct(string $username, string $password)
{
$this->username = $username;
$this->password = $password;
}
public function getUsername()
{
return $this->username;
}
public function getPassword()
{
return $this->password;
}
}
?>

@ -0,0 +1,13 @@
<h1>
<?php echo $row['title'] ?>
</h1>
<?php
echo $row['description'];
echo "<br>";
echo $row['pubdate'];
echo "<br>";
echo $row['link'];
echo "<br>";
?>

@ -0,0 +1,32 @@
<?php
class Connection extends PDO {
private $stmt;
public function __construct(string $dsn, string $username, string $password) {
parent::__construct($dsn,$username,$password);
$this->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();
}
}
?>

@ -0,0 +1,23 @@
<?php
require_once("admins.php");
class GatewayAdmin
{
private $con;
public function __construct($con){
$this->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)
)
);
}
}
?>

@ -0,0 +1,34 @@
<?php
require_once("news.php");
class GatewayNews
{
private $con;
public function __construct($con){
$this->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;
}
}
?>

@ -0,0 +1,44 @@
<html>
<body>
<?php
require_once("connection.php");
require_once("gatewayNews.php");
require_once("gatewayAdmins.php");
$user= 'jeducourth';
$pass='achanger';
$dsn='mysql:host=localhost;dbname=dbjeducourth';
$title="titre";
$description="descirption du titre";
$pubdate="2022-04-12 8:8:8";
$link="exemple.com";
try{
$con = new Connection($dsn,$user,$pass);
$gateNews = new GatewayNews($con);
$gateNews->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()."<br>".$new->getDescription()."<br>".$new->getPubDate()."<br>".$new->getLink()."<br><br>";
}*/
require("listeNews.php");
}
catch( PDOException $Exception ) {
echo 'erreur';
echo $Exception->getMessage();
}
?>
</body>
</html>

@ -0,0 +1,13 @@
<?php
$results=$gateNews->getNews();
Foreach ($results as $new){
print $new->getTitle()."<br>";
print $new->getDescription()."<br>";
print $new->getPubDate()."<br>";
print "<a href=";
print $new->getLink().">";
print $new->getLink()."</a><br>";
print "<br>";
}
?>

@ -0,0 +1,37 @@
<?php
class News
{
private string $title;
private string $description;
private string $pubdate;
private string $link;
public function __construct(string $title, string $description, string $pubdate, string $link)
{
$this->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;
}
}
?>
Loading…
Cancel
Save