how can I show all the users in the users table in the database in a html table? I have to show all the users in a html table and each user can be selected using a check box. The selected users are then deleted from the database. I am using the technique mvc and I have a page "DeleteUser" that contains only the html, a controller and a public file.
View/DeleteUser.php contains:
<form method="post" action="../public/deleteUser.php">
<table>
<tr>
<td>#</td>
<td>Username</td>
<td>First name</td>
<td>Last name</td>
<td>City</td>
<td>Gender</td>
<td>E-mail</td>
<td>Check</td>
</tr>
<tr>
<td><input type="submit" name="submit-deleteuser" value="Delete user"/></td>
</tr>
</table>
</form>
public/deleteUser.php contains:
$controller = new DeleteUserController();
$view = $controller->invoke();
$view->render();
The invoke() method in Controller/DeleteUserController.php contains:
class DeleteUserController {
public $user;
public $users;
public $userRepository;
public $view;
public function __construct() {
$username = $_SESSION['username'];
$this->userRepository = new UserRepository();
$this->user = $this->userRepository->findByUsername($username);
$this->users = array();
$this->view = new View('DeleteUser');
$db = Database::getInstance();
}
public function listUsers() {
$this->users = $this->userRepository->fetchAll();
foreach($this->users as $u) {
$str = "<tr>
<td><?php $u->getId() ?></td>
</tr>";
}
}
public function deleteUsers() {
if(isset($_POST['submit-deleteuser'])) {
$del_id = $_POST['users'];
foreach($del_id as $value) {
$value->delete();
}
}
}
public function work() {
$this->listUsers();
$this->deleteUsers();
}
public function invoke() {
$this->work();
$this->view->setData('user', $this->user);
return $this->view;
}
If I had not had to use mvc I would have done so:
<table>
<tr>
<td>#</td>
<td>Username</td>
<td>Fisrt name</td>
<td>Last name</td>
<td>City</td>
<td>Gender</td>
<td>E-mail</td>
<td>Check</td>
</tr>
<?php
$utente = $_SESSION['username'];
$query = "SELECT * FROM users ORDER BY id";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
echo "<tr>
<td>$row[id]</td>
<td>$row[username]</td>
<td>$row[fisrt_name]</td>
<td>$row[last_name]</td>
<td>$row[city]</td>
<td>$row[gender]</td>
<td>$row[email]</td>
<td><input type=\"checkbox\" name=\"users[]\" value=\"" .$row['id']. "\"></td>
</tr>";
}
}
?>
<tr>
<td colspan="9"><input type="submit" name="submit-deleteuser" value="Delete users"/></td>
</tr>
</table>
But now I do not know how to do. The two methods listUsers() and deleteUsers() are wrong and do not know how to complete them or correct them.
First it was simple, I put the php code that showed users in the html tag that I wanted (<table>...</table>
) but now?
I do not know how to handle this thing.
Give me advice? Thank you :)
fetchAll() is:
public function fetchAll(){
$db = Database::getInstance();
$stmt = $db->prepare("SELECT * FROM `users`");
$stmt->execute();
$users = array();
while($result = $stmt->fetch()) {
$users[] = User::load($result);
}
return $users;
}