Commit 6522fb03 authored by Quentin Vrel's avatar Quentin Vrel

tp1 suite et fin

parent eadb989f
<?php
require_once("initPDO.php");
class User{
/*private static function _initInstances(){
global $pdo;
if (!isset(self::$_instances)) {
$request = $pdo->prepare("select * from users");
$request->execute();
self::$_instances = [];
while($line = $request->fetch()){
$_instances[] = $line;
}
print_r($_instances);
}
}*/
private static function getAllUsers(){
global $pdo;
$request = $pdo->prepare("select * from users");
$request->execute();
$request->setFetchMode(PDO::FETCH_CLASS, get_called_class());
$users = $request->fetchAll();
return $users;
}
public static function showAllUsersAsTable(){
$str="<table>
<tr>
<th>User</th>
<th>email</th>
</tr>";
foreach (static::getAllUsers() as $value) {
$str.=$value->toHtml();
}
$str.='
</body>
</html>';
echo $str;
}
private function toHtml(){
return "<tr>
<td>$this->name</th>
<td>$this->email</th>
</tr>";
}
}
\ No newline at end of file
<?php
// initialise une variable $pdo connecté à la base locale
require_once("initPDO.php"); // cf. doc / cours
$request = $pdo->prepare("select * from users");
// à vous de compléter...
// afficher un tableau HTML avec les donnéees en utilisant fetch(PDO::FETCH_OBJ)
if(isset($_POST['name']) && isset($_POST['mail'])){
$insert = $pdo->prepare("INSERT INTO `users` (`id`, `name`, `email`) VALUES (NULL, '".$_POST['name']."', '".$_POST['mail']."');");
$insert->execute();
}
$request->execute();
?>
<html>
<body>
<h1> Users</h1>
<table>
<tr>
<th>User</th>
<th>email</th>
</tr>
<?php
while($line = $request->fetch()){
//var_dump($line);
echo "<tr>
<td>".$line['name']."</th>
<td>".$line['email']."</th>
</tr>";
}
<h1>Users</h1>
<?php
require_once("test-PDO-class.php");
/*** close the database connection ***/
$pdo = null;
?>
</table>
User::showAllUsersAsTable();
?>
<form target="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Name:
<input type="text" id="name" name="name"/><br>
Mail:
<input type="text" id="mail" name="mail"/><br>
<input type="submit" value="Ajouter">
</form>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment