Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
projet-cdaw
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Quentin Vrel
projet-cdaw
Commits
6522fb03
Commit
6522fb03
authored
Nov 10, 2020
by
Quentin Vrel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tp1 suite et fin
parent
eadb989f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
45 deletions
+54
-45
test-PDO-class.php
BackEnd/tp1/test-PDO-class.php
+48
-0
test-PDO.php
BackEnd/tp1/test-PDO.php
+6
-45
No files found.
BackEnd/tp1/test-PDO-class.php
0 → 100644
View file @
6522fb03
<?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
BackEnd/tp1/test-PDO.php
View file @
6522fb03
<?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>"
;
}
/*** close the database connection ***/
$pdo
=
null
;
?>
</table>
<h1>
Users
</h1>
<?php
require_once
(
"test-PDO-class.php"
);
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment