Skip to content
Snippets Groups Projects
Commit 695224db authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

Reformatage du code et préparation au TP 7.

parent f9c23a7d
Branches
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/TP2/ex4/conn.php" dialect="GenericSQL" />
<file url="PROJECT" dialect="MariaDB" />
</component>
</project>
\ No newline at end of file
# TP-PHP-LPPRISM
## Information pour M.Gagné
Je suis désolé, mais j'ai vraiment du mal avec votre façon de faire. Les pratiques réalisées sur le TP5 ne sont pas
bonnes pour plusieurs raisons.
**Premièrement**, même si je comprends bien que créer plusieurs contrôleurs est rébarbatif, et qu'il peut être intéressant
de toute centralisé comme nous l'avions fait avec les vues, ici ce n'est pas une bonne idée. Le problème étant que les
tables gérées par les modèles n'ont pas les mêmes noms de méthodes, ni de propriétés, il serait difficile de créer un
contrôleur unique pour chaque modèle sans devoir implémenter du code spécifique.
Bien sûr, nous pourrions toujours les renommer en un nom plus général, me diriez-vous. Ainsi on règlerait le problème des noms de méthodes
comme `getAllAuteurs()`. Cela est d'ailleurs compatible avec les pages individuelles. Mais cela ne corrigera pas le problème du nom des propriétés.
En effet, si pour un livre nous allons utiliser son année de parution dans sa description, pour un auteur nous allons plutôt
utiliser son année de naissance. Même en supprimant les descriptions superflues que j'ai ajouté aux liens, il reste la
question de pouvoir récupérer l'identifiant unique de chaque objet. Encore une fois, les noms sont différents.
Nous pourrions encore et toujours trouver une solution, mais cela ne fera que contourner le problème qui est que
centraliser tous les contrôleurs de ces contrôleurs, en sachant que nous n'avons qu'une seule vue, n'est pas une bonne
idée.
**Nous en venons au deuxième problème**. Avoir centralisé les vues est une très bonne idée. Cependant, cela signife que
leur contenu doit être géré dans les contrôleurs. Cela ne devrait pas poser de problème, sauf si nous avons décidé de
centraliser aussi. Pour palier à ce problème, il faudrait alors revoir la manières dont sont créés les modèles. Je
suppose qu'il s'agit là du sujet des prochains TP.
\ No newline at end of file
File deleted
<?php
echo "Hello world!";
\ No newline at end of file
<?php
$today = date('H:i:s, d-m-Y');
echo "Nous sommes le: " . $today;
\ No newline at end of file
<?php
var_dump(getdate());
\ No newline at end of file
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>TP1 - ex4</title>
<link rel="icon" type="image/png" href="iconPHP.png">
</head>
<body>
<h2>Palmarès de la ligue des champions</h2>
<?php
$palmares = array(
2022 => "Real Madrid", 2021 => "Chelsea", 2020 => "Bayern Munich",
2019 => "Liverpool", 2018 => "Real Madrid", 2017 => "Real Madrid",
2016 => "Real Madrid", 2015 => "Barcelone", 2014 => "Real Madrid",
2013 => "Bayern Munich", 2012 => "Chelsea", 2011 => "Barcelone",
2010 => "Inter Milan", 2009 => "Barcelone", 2008 => "Manchester United",
2007 => "AC Milan", 2006 => "Barcelone", 2005 => "Liverpool",
2004 => "Porto", 2003 => "AC Milan", 2002 => "Real Madrid",
2001 => "Bayern Munich", 2000 => "Real Madrid"
);
echo "<pre>";
print_r($palmares);
echo "</pre>";
$vainqueurs = array();
foreach ($palmares as $personne){
if(!isset($vainqueurs[$personne])){
$vainqueurs[$personne] = 1;
}else{
$vainqueurs[$personne]++;
}
}
ksort($vainqueurs);
echo "<h2>Vainqueurs triés</h2>";
echo "<pre>";
print_r($vainqueurs);
echo "</pre>";
?>
</body>
</html>
<?php
$capital = array(10000);
for ($i=0; $i<10; $i++){
echo "<br>$i: " . $capital[$i];
$capital[$i+1] = floor($capital[$i] * 1.05);
}
\ No newline at end of file
<?php
$atteindre = 18000;
$init = 10000;
$taux = 1.05;
$total = $init;
$nbAnnees = 0;
while($total < $atteindre){
$total *= $taux;
$nbAnnees++;
}
?>
<h1>Dépassement d'un seuil donné</h1>
<p>Seuil à atteindre : <?=$atteindre?></p>
<p>Captial Intial : <?=$init?></p>
<p>Taux d'intérêt : <?php echo ($taux - 1) * 100; ?> %</p>
<p>Capital en début d'année <?=$nbAnnees?> : <?=$total?></p>
<?php
if(isset($_GET['seuil'])){
$atteindre = $_GET['seuil'];
}else{
$atteindre = 18000;
}
if(isset($_GET['capitalInitial'])){
$init = $_GET['capitalInitial'];
}else{
$init = 10000;
}
if(isset($_GET['taux'])){
$taux = $_GET['taux']/100 + 1;
}else{
$taux = 1.05;
}
$total = $init;
$nbAnnees = 0;
while($total < $atteindre){
$total *= $taux;
$nbAnnees++;
}
?>
<h1>Dépassement d'un seuil donné</h1>
<p>Seuil à atteindre : <?=$atteindre?></p>
<p>Captial Intial : <?=$init?></p>
<p>Taux d'intérêt : <?php echo ($taux - 1) * 100; ?> %</p>
<p>Capital en début d'année <?=$nbAnnees?> : <?=$total?></p>
\ No newline at end of file
<?php
if(isset($_GET['seuil'])){
$atteindre = $_GET['seuil'];
}else{
$atteindre = 18000;
}
if(isset($_GET['capitalInitial'])){
$init = $_GET['capitalInitial'];
}else{
$init = 10000;
}
if(isset($_GET['targetYears'])){
$targetYears = $_GET['targetYears'];
}else{
$targetYears = 10;
}
$taux = ($atteindre - $init) / $targetYears;
$total = $init;
$nbAnnees = 0;
while($total < $atteindre){
$total *= $taux;
$nbAnnees++;
}
?>
<h1>Dépassement d'un seuil donné</h1>
<p>Seuil à atteindre : <?=$atteindre?></p>
<p>Captial Intial : <?=$init?></p>
<p>Taux d'intérêt pour <?=$targetYears?> année(s) : <?php echo ($taux - 1) * 100; ?> %</p>
<p>Capital en début d'année <?=$nbAnnees?> : <?=$total?></p>
\ No newline at end of file
<h1>Affichage de $_GET</h1>
<?php
echo "<pre>"; print_r($_GET); echo "</pre>";
?>
<h1>Affichage des index de $_GET</h1>
<?php
echo "<pre>"; print_r(array_keys($_GET)); echo "</pre>";
echo "<h1>Détection des clés prenom et nom</Affi></h1>";
if(array_key_exists("prenom", $_GET)){
echo "Prénom: " . $_GET['prenom'];
}
if(array_key_exists("nom", $_GET)){
echo "<br>Nom: " . $_GET['nom'];
}
?>
File deleted
<?php
class Auteur
{
private int $numAuteur;
private string $nom;
private string $prenom;
private string $nationalite;
private int $anneeNaissance;
/**
* @param int $numAuteur
* @param string|null $nom
* @param string|null $prenom
* @param string|null $nationalite
* @param int|null $anneeNaissance
*/
public function __construct(int $numAuteur, string $nom = null, string $prenom = null, string $nationalite = null, int $anneeNaissance = null)
{
$this->numAuteur = $numAuteur;
if($nom){
$this->nom = $nom;
$this->prenom = $prenom;
$this->nationalite = $nationalite;
$this->anneeNaissance = $anneeNaissance;
}
}
/**
* @return int
*/
public function getNumAuteur(): int
{
return $this->numAuteur;
}
/**
* @param int $numAuteur
*/
public function setNumAuteur(int $numAuteur): void
{
$this->numAuteur = $numAuteur;
}
/**
* @return string
*/
public function getNom(): string
{
return $this->nom;
}
/**
* @param string $nom
*/
public function setNom(string $nom): void
{
$this->nom = $nom;
}
/**
* @return string
*/
public function getPrenom(): string
{
return $this->prenom;
}
/**
* @param string $prenom
*/
public function setPrenom(string $prenom): void
{
$this->prenom = $prenom;
}
/**
* @return string
*/
public function getNationalite(): string
{
return $this->nationalite;
}
/**
* @param string $nationalite
*/
public function setNationalite(string $nationalite): void
{
$this->nationalite = $nationalite;
}
/**
* @return int
*/
public function getAnneeNaissance(): int
{
return $this->anneeNaissance;
}
/**
* @param int $anneeNaissance
*/
public function setAnneeNaissance(int $anneeNaissance): void
{
$this->anneeNaissance = $anneeNaissance;
}
/**
* @return void
*/
public function afficher(): void
{
echo "<p>Nom: " . $this->nom . " Prénom: " . $this->prenom . "</p>";
}
}
\ No newline at end of file
<?php
require "Auteur.php";
$aut1 = new Auteur(1);
$aut2 = new Auteur(2, "Clavier", "Christian", "France", 1969);
$aut1->afficher();
$aut2->afficher();
\ No newline at end of file
<?php
class Auteur
{
private int $numAuteur;
private string $nom;
private string $prenom;
private string $nationalite;
private int $anneeNaissance;
/**
* @param int $numAuteur
* @param string|null $nom
* @param string|null $prenom
* @param string|null $nationalite
* @param int|null $anneeNaissance
*/
public function __construct(int $numAuteur, string $nom = null, string $prenom = null, string $nationalite = null, int $anneeNaissance = null)
{
$this->numAuteur = $numAuteur;
if($nom){
$this->nom = $nom;
$this->prenom = $prenom;
$this->nationalite = $nationalite;
$this->anneeNaissance = $anneeNaissance;
}else{
$bdd = new Database;
$bdd->connect();
$bdd = $bdd->pdo();
$query = $bdd->prepare("SELECT * FROM auteur WHERE numAuteur = ?");
$query->execute([$numAuteur]);
$data = $query->fetch(PDO::FETCH_ASSOC);
$this->nom = $data['nom'];
$this->prenom = $data['prenom'];
$this->nationalite = $data['nationalite'];
$this->anneeNaissance = $data['anneeNaissance'];
}
}
/**
* @return int
*/
public function getNumAuteur(): int
{
return $this->numAuteur;
}
/**
* @param int $numAuteur
*/
public function setNumAuteur(int $numAuteur): void
{
$this->numAuteur = $numAuteur;
}
/**
* @return string
*/
public function getNom(): string
{
return $this->nom;
}
/**
* @param string $nom
*/
public function setNom(string $nom): void
{
$this->nom = $nom;
}
/**
* @return string
*/
public function getPrenom(): string
{
return $this->prenom;
}
/**
* @param string $prenom
*/
public function setPrenom(string $prenom): void
{
$this->prenom = $prenom;
}
/**
* @return string
*/
public function getNationalite(): string
{
return $this->nationalite;
}
/**
* @param string $nationalite
*/
public function setNationalite(string $nationalite): void
{
$this->nationalite = $nationalite;
}
/**
* @return int
*/
public function getAnneeNaissance(): int
{
return $this->anneeNaissance;
}
/**
* @param int $anneeNaissance
*/
public function setAnneeNaissance(int $anneeNaissance): void
{
$this->anneeNaissance = $anneeNaissance;
}
/**
* @return void
*/
public function afficher(): void
{
echo "<p>Nom: " . $this->nom . " Prénom: " . $this->prenom . "</p>";
}
public static function getAllAuteurs(): array
{
$bdd = new Database;
$bdd->connect();
$bdd = $bdd->pdo();
$query = $bdd->query("SELECT * FROM auteur");
$data = $query->fetchAll(PDO::FETCH_ASSOC);
$array = array();
foreach ($data as $auteur){
$array[] = new Auteur($auteur['numAuteur']);
}
return $array;
}
}
\ No newline at end of file
<?php
class Connexion
{
// attributs de la classe Connexion paramètres de connexion à la base
static private $hostname = 'localhost';
static private $database = 'iut-dev';
static private $login = 'iut-dev-user';
static private $password = 'p73i74KAV8lami2iyIpehE5ozic8GA';
// attribut de la classe Connexion paramètres d'encodage
static private $tabUTF8 = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");
// attribut de la classe Connexion qui recevra l'instance PDO
static private $pdo;
// getter de pdo
static public function pdo()
{
return self::$pdo;
}
// fonction de connexion
static public function connect()
{
$h = self::$hostname;
$d = self::$database;
$l = self::$login;
$p = self::$password;
$t = self::$tabUTF8;
try {
self::$pdo = new PDO("mysql:host=$h;dbname=$d", $l, $p, $t);
self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Erreur de connexion !";
}
}
}
\ No newline at end of file
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require "init.php";
require "process.php";
?>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Test formulaire</title>
</head>
<body>
<h1>Voici la liste des utilisateurs</h1>
<?php
foreach ($auteurs as $auteur){
$auteur->afficher();
}
?>
<h1>Ajouter un auteur</h1>
<form action="" method="post">
<label>
Nom
<input type="text" name="nom" placeholder="Nom" required>
</label>
<label>
Prénom
<input type="text" name="prenom" placeholder="Prenom" required>
</label>
<label>
Nationalité
<input type="text" name="nationalite" placeholder="Nationalité" required>
</label>
<label>
Année de naissance
<input type="number" name="anneeNaissance" placeholder="Année de naissance" required>
</label>
<button type="submit">Créer un auteur</button>
</form>
</body>
</html>
\ No newline at end of file
<?php
require "conn.php";
require "Auteur.php";
$createTable = "CREATE TABLE IF NOT EXISTS auteur ( `numAuteur` INT NOT NULL AUTO_INCREMENT , `nom` VARCHAR(64) NOT NULL , `prenom` VARCHAR(64) NOT NULL , `nationalite` VARCHAR(32) NOT NULL , `anneeNaissance` INT NOT NULL , PRIMARY KEY (`numAuteur`)) ENGINE = InnoDB;";
$bdd = new Database;
$bdd->connect();
$bdd = $bdd->pdo();
$query = $bdd->query($createTable);
<?php
if(!empty($_POST)){
if(!empty($_POST['nom']) && !empty($_POST['prenom']) && !empty($_POST['nationalite']) && !empty($_POST['anneeNaissance'])){
$bdd = new Database;
$bdd->connect();
$bdd = $bdd->pdo();
$query = $bdd->prepare("INSERT INTO auteur (nom, prenom, nationalite, anneeNaissance) VALUE (?,?,?,?)");
$query->execute([$_POST['nom'], $_POST['prenom'], $_POST['nationalite'], $_POST['anneeNaissance']]);
echo "Auteur ajouté";
}else{
echo "Données manquantes pour ajouter l'auteur.";
}
}
$auteurs = Auteur::getAllAuteurs();
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment